-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD sort-algos,sort-algos-int,sort-algos-int-with-func
- Loading branch information
ismdeep
committed
Jan 30, 2020
1 parent
39aa50a
commit b25682e
Showing
32 changed files
with
618 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
include/sort-algos-int-with-func/bubble-sort-int-with-func.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int-with-func/bubble-sort-int-with-func.h> | ||
|
||
#include <data-swap.h> | ||
|
||
void bubble_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) { | ||
for (int stop = len - 1; stop > 0; stop--) { | ||
for (int i = 0; i < stop; i++) { | ||
if ( !cmp_func(&a[i], &a[i+1])) { | ||
data_swap(a + i, a + i + 1, sizeof(int)); | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
include/sort-algos-int-with-func/bubble-sort-int-with-func.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H | ||
|
||
#include <stdbool.h> | ||
|
||
void bubble_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H |
26 changes: 26 additions & 0 deletions
26
include/sort-algos-int-with-func/cocktail-sort-int-with-func.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int-with-func/cocktail-sort-int-with-func.h> | ||
|
||
void cocktail_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) { | ||
int i, left = 0, right = len - 1; | ||
int temp; | ||
while (left < right) { | ||
for (i = left; i < right; i++) | ||
if (!cmp_func(&a[i], &a[i + 1])) { | ||
temp = a[i]; | ||
a[i] = a[i + 1]; | ||
a[i + 1] = temp; | ||
} | ||
right--; | ||
for (i = right; i > left; i--) | ||
if (!cmp_func(&a[i - 1], &a[i])) { | ||
temp = a[i]; | ||
a[i] = a[i - 1]; | ||
a[i - 1] = temp; | ||
} | ||
left++; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
include/sort-algos-int-with-func/cocktail-sort-int-with-func.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H | ||
|
||
#include <stdbool.h> | ||
|
||
void cocktail_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H |
23 changes: 23 additions & 0 deletions
23
include/sort-algos-int-with-func/insertion-sort-int-with-func.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int-with-func/insertion-sort-int-with-func.h> | ||
|
||
#include <stdbool.h> | ||
|
||
void insertion_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) { | ||
for (int left = 0; left < len; left++) { | ||
int min_p = left; | ||
for (int cur = min_p + 1; cur < len; cur++) { | ||
if ( cmp_func(&a[cur], &a[min_p]) ) { | ||
min_p = cur; | ||
} | ||
} | ||
int tmp = a[min_p]; | ||
for (int cursor = min_p - 1; cursor >= left; cursor--) { | ||
a[cursor + 1] = a[cursor]; | ||
} | ||
a[left] = tmp; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
include/sort-algos-int-with-func/insertion-sort-int-with-func.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H | ||
|
||
#include <stdbool.h> | ||
|
||
void insertion_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H |
32 changes: 32 additions & 0 deletions
32
include/sort-algos-int-with-func/quick-sort-int-with-func.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int-with-func/quick-sort-int-with-func.h> | ||
|
||
#include <stdbool.h> | ||
#include <data-swap.h> | ||
|
||
void quick_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) { | ||
if (len <= 1) | ||
return; | ||
int mid = a[len - 1]; | ||
int left = 0; | ||
int right = len - 2; | ||
while (left < right) { | ||
while (cmp_func(&a[left], &mid) && left < right) | ||
++left; | ||
while (!cmp_func(&a[right], &mid) && left < right) | ||
--right; | ||
data_swap(a + left, a + right, sizeof(int)); | ||
} | ||
if ( cmp_func(&a[len-1], &a[left]) ) { | ||
data_swap(a + left, a + len - 1, sizeof(int)); | ||
} else { | ||
++left; | ||
data_swap(a + left, a + len - 1, sizeof(int)); | ||
} | ||
if (left) | ||
quick_sort(a, left, cmp_func); | ||
quick_sort(a + left + 1, len - left - 1, cmp_func); | ||
} |
12 changes: 12 additions & 0 deletions
12
include/sort-algos-int-with-func/quick-sort-int-with-func.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H | ||
|
||
#include <stdbool.h> | ||
|
||
void quick_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H |
21 changes: 21 additions & 0 deletions
21
include/sort-algos-int-with-func/selection-sort-int-with-func.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int-with-func/selection-sort-int-with-func.h> | ||
|
||
#include <stdbool.h> | ||
|
||
void selection_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)) { | ||
for (int left = 0; left < len; left++) { | ||
int min_p = left; | ||
for (int cur = left + 1; cur < len; cur++) { | ||
if ( cmp_func(&a[cur], &a[min_p]) ) { | ||
min_p = cur; | ||
} | ||
} | ||
int tmp = a[min_p]; | ||
a[min_p] = a[left]; | ||
a[left] = tmp; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
include/sort-algos-int-with-func/selection-sort-int-with-func.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H | ||
|
||
#include <stdbool.h> | ||
|
||
void selection_sort(int a[], int len, bool (*cmp_func)(const int *, const int *)); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Created by ismdeep on 2020/1/30. | ||
// | ||
|
||
#include <sort-algos-int-with-func/sort-test.h> | ||
|
||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <sys/time.h> | ||
#include <sorted-assert.h> | ||
#include <time-utils.h> | ||
#include <stdio.h> | ||
|
||
void sort_test( | ||
int data[], | ||
int len, | ||
void (*sort_func)(int [], int, bool(*)(const int*, const int*)), | ||
char *sort_func_name, | ||
bool (*cmp_func)(const int*, const int*) | ||
) { | ||
/* Copy data from raw to selection_sort_data */ | ||
int *sort_data = (int *) malloc(sizeof(int) * len ); | ||
memcpy(sort_data, data, sizeof(int) * len); | ||
|
||
/* Sort array **sort_data** with **sort_func()** algorithm */ | ||
struct timeval *start_point = create_start_point(); | ||
sort_func(sort_data, len, cmp_func); | ||
printf("Time elapse[%s]: %.2lf ms\n", sort_func_name, stop_watch_us(start_point) / 1000.0); | ||
|
||
/* Assert sort result */ | ||
if (sorted_assert(data, data + len, sort_data, sort_data + len, sizeof(int), | ||
(bool (*)(const void *, const void *)) cmp_func)) { | ||
printf("Successfully.\n\n"); | ||
} else { | ||
printf("Unsuccessfully.\n\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Created by ismdeep on 2020/1/30. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H | ||
|
||
#include <stdbool.h> | ||
|
||
void sort_test( | ||
int data[], | ||
int len, | ||
void (*sort_func)(int [], int, bool(*)(const int*, const int*)), | ||
char *sort_func_name, | ||
bool (*cmp_func)(const int*, const int*) | ||
); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int/bubble-sort-int.h> | ||
#include <data-swap.h> | ||
|
||
|
||
void bubble_sort(int a[], int len) { | ||
for (int stop = len - 1; stop > 0; stop--) { | ||
for (int i = 0; i < stop; i++) { | ||
if (a[i] > a[i + 1]) { | ||
data_swap(a + i, a + i + 1, sizeof(int)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_C_BUBBLE_SORT_INT_H | ||
#define SORT_ALGOS_C_BUBBLE_SORT_INT_H | ||
|
||
void bubble_sort(int a[], int len); | ||
|
||
#endif //SORT_ALGOS_C_BUBBLE_SORT_INT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int/cocktail-sort-int.h> | ||
|
||
void cocktail_sort(int a[], int len) { | ||
int i, left = 0, right = len - 1; | ||
int temp; | ||
while (left < right) { | ||
for (i = left; i < right; i++) | ||
if (a[i] > a[i + 1]) { | ||
temp = a[i]; | ||
a[i] = a[i + 1]; | ||
a[i + 1] = temp; | ||
} | ||
right--; | ||
for (i = right; i > left; i--) | ||
if (a[i - 1] > a[i]) { | ||
temp = a[i]; | ||
a[i] = a[i - 1]; | ||
a[i - 1] = temp; | ||
} | ||
left++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H | ||
|
||
void cocktail_sort(int a[], int len); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#include <sort-algos-int/insertion-sort-int.h> | ||
|
||
void insertion_sort(int a[], int len) { | ||
for (int left = 0; left < len; left++) { | ||
int min_p = left; | ||
for (int cur = min_p + 1; cur < len; cur++) { | ||
if ( a[cur] < a[min_p] ) { | ||
min_p = cur; | ||
} | ||
} | ||
int tmp = a[min_p]; | ||
for (int cursor = min_p - 1; cursor >= left; cursor--) { | ||
a[cursor + 1] = a[cursor]; | ||
} | ||
a[left] = tmp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// Created by ismdeep on 2020/1/28. | ||
// | ||
|
||
#ifndef SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H | ||
#define SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H | ||
|
||
void insertion_sort(int a[], int len); | ||
|
||
#endif //SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H |
Oops, something went wrong.