├── .gitignore ├── include ├── sort-algos-int │ ├── bubble-sort-int.h │ ├── quick-sort-int.h │ ├── cocktail-sort-int.h │ ├── insertion-sort-int.h │ ├── selection-sort-int.h │ ├── bubble-sort-int.c │ ├── selection-sort-int.c │ ├── sort-test.h │ ├── insertion-sort-int.c │ ├── cocktail-sort-int.c │ ├── quick-sort-int.c │ └── sort-test.c ├── data-swap.h ├── sort-algos │ ├── selection-sort.h │ ├── quick-sort.h │ ├── bubble-sort.h │ ├── cocktail-sort.h │ ├── insertion-sort.h │ ├── sort-test.h │ ├── bubble-sort.c │ ├── selection-sort.c │ ├── cocktail-sort.c │ ├── insertion-sort.c │ ├── quick-sort.c │ └── sort-test.c ├── data-swap.c ├── time-utils.h ├── sort-algos-int-with-func │ ├── quick-sort-int-with-func.h │ ├── bubble-sort-int-with-func.h │ ├── cocktail-sort-int-with-func.h │ ├── insertion-sort-int-with-func.h │ ├── selection-sort-int-with-func.h │ ├── sort-test.h │ ├── bubble-sort-int-with-func.c │ ├── selection-sort-int-with-func.c │ ├── insertion-sort-int-with-func.c │ ├── cocktail-sort-int-with-func.c │ ├── quick-sort-int-with-func.c │ └── sort-test.c ├── sorted-assert.h ├── sort-base.h ├── time-utils.c └── sorted-assert.c ├── README.md ├── LICENSE ├── sort-algos-int.c ├── sort-algos-int-with-func.c ├── sort-algos.c └── CMakeLists.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | cmake-build-debug 3 | -------------------------------------------------------------------------------- /include/sort-algos-int/bubble-sort-int.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_BUBBLE_SORT_INT_H 6 | #define SORT_ALGOS_C_BUBBLE_SORT_INT_H 7 | 8 | void bubble_sort(int a[], int len); 9 | 10 | #endif //SORT_ALGOS_C_BUBBLE_SORT_INT_H 11 | -------------------------------------------------------------------------------- /include/sort-algos-int/quick-sort-int.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_H 7 | 8 | void quick_sort(int a[], int len); 9 | 10 | #endif //SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_H 11 | -------------------------------------------------------------------------------- /include/data-swap.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_DATA_SWAP_H 6 | #define SORT_ALGOS_C_DATA_SWAP_H 7 | 8 | #include 9 | #include 10 | 11 | void data_swap(void *data1, void *data2, size_t item_size); 12 | 13 | #endif //SORT_ALGOS_C_DATA_SWAP_H 14 | -------------------------------------------------------------------------------- /include/sort-algos-int/cocktail-sort-int.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H 7 | 8 | void cocktail_sort(int a[], int len); 9 | 10 | #endif //SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_H 11 | -------------------------------------------------------------------------------- /include/sort-algos-int/insertion-sort-int.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H 7 | 8 | void insertion_sort(int a[], int len); 9 | 10 | #endif //SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_H 11 | -------------------------------------------------------------------------------- /include/sort-algos-int/selection-sort-int.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_H 7 | 8 | void selection_sort(int a[], int len); 9 | 10 | #endif //SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_H 11 | -------------------------------------------------------------------------------- /include/sort-algos/selection-sort.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_SELECTION_SORT_H 6 | #define SORT_ALGOS_C_SELECTION_SORT_H 7 | 8 | #include 9 | #include 10 | 11 | void selection_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func); 12 | 13 | 14 | #endif //SORT_ALGOS_C_SELECTION_SORT_H 15 | -------------------------------------------------------------------------------- /include/data-swap.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | void data_swap(void *data1, void *data2, size_t item_size) { 11 | void *tmp = malloc(item_size); 12 | memcpy(tmp, data1, item_size); 13 | memcpy(data1, data2, item_size); 14 | memcpy(data2, tmp, item_size); 15 | free(tmp); 16 | } 17 | -------------------------------------------------------------------------------- /include/time-utils.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_TIME_UTILS_H 6 | #define SORT_ALGOS_C_TIME_UTILS_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | struct timeval *create_start_point(); 14 | 15 | uint64_t stop_watch_us(const struct timeval *t1); 16 | 17 | #endif //SORT_ALGOS_C_TIME_UTILS_H 18 | -------------------------------------------------------------------------------- /include/sort-algos/quick-sort.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_QUICK_SORT_H 6 | #define SORT_ALGOS_C_QUICK_SORT_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void quick_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func); 14 | 15 | #endif //SORT_ALGOS_C_QUICK_SORT_H 16 | -------------------------------------------------------------------------------- /include/sort-algos/bubble-sort.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_BUBBLE_SORT_H 6 | #define SORT_ALGOS_C_BUBBLE_SORT_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void bubble_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func); 14 | 15 | #endif //SORT_ALGOS_C_BUBBLE_SORT_H 16 | -------------------------------------------------------------------------------- /include/sort-algos/cocktail-sort.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_COCKTAIL_SORT_H 6 | #define SORT_ALGOS_C_COCKTAIL_SORT_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void cocktail_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func); 14 | 15 | #endif //SORT_ALGOS_C_COCKTAIL_SORT_H 16 | -------------------------------------------------------------------------------- /include/sort-algos/insertion-sort.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_INSERTION_SORT_H 6 | #define SORT_ALGOS_C_INSERTION_SORT_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void insertion_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func); 14 | 15 | #endif //SORT_ALGOS_C_INSERTION_SORT_H 16 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/quick-sort-int-with-func.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H 7 | 8 | #include 9 | #include 10 | 11 | void quick_sort(int a[], int len, cmp_int_func_t cmp_func); 12 | 13 | #endif //SORT_ALGOS_INT_WITH_FUNC_QUICK_SORT_INT_WITH_FUNC_H 14 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/bubble-sort-int-with-func.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H 7 | 8 | #include 9 | #include 10 | 11 | void bubble_sort(int a[], int len, cmp_int_func_t cmp_func); 12 | 13 | #endif //SORT_ALGOS_INT_WITH_FUNC_BUBBLE_SORT_INT_WITH_FUNC_H 14 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/cocktail-sort-int-with-func.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H 7 | 8 | #include 9 | #include 10 | 11 | void cocktail_sort(int a[], int len, cmp_int_func_t cmp_func); 12 | 13 | #endif //SORT_ALGOS_INT_WITH_FUNC_COCKTAIL_SORT_INT_WITH_FUNC_H 14 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/insertion-sort-int-with-func.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H 7 | 8 | #include 9 | #include 10 | 11 | void insertion_sort(int a[], int len, cmp_int_func_t cmp_func); 12 | 13 | #endif //SORT_ALGOS_INT_WITH_FUNC_INSERTION_SORT_INT_WITH_FUNC_H 14 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/selection-sort-int-with-func.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H 7 | 8 | #include 9 | #include 10 | 11 | void selection_sort(int a[], int len, cmp_int_func_t cmp_func); 12 | 13 | #endif //SORT_ALGOS_INT_WITH_FUNC_SELECTION_SORT_INT_WITH_FUNC_H 14 | -------------------------------------------------------------------------------- /include/sort-algos-int/bubble-sort-int.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | 9 | void bubble_sort(int a[], int len) { 10 | for (int stop = len - 1; stop > 0; stop--) { 11 | for (int i = 0; i < stop; i++) { 12 | if (a[i] > a[i + 1]) { 13 | data_swap(a + i, a + i + 1, sizeof(int)); 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/sort-test.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/30. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H 7 | 8 | #include 9 | #include 10 | 11 | void sort_test( 12 | int data[], 13 | int len, 14 | sort_int_func_t sort_func, 15 | char *sort_func_name, 16 | cmp_int_func_t cmp_func 17 | ); 18 | 19 | #endif //SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H 20 | -------------------------------------------------------------------------------- /include/sort-algos/sort-test.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_SORT_TEST_H 6 | #define SORT_ALGOS_C_SORT_TEST_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | void sort_test( 13 | void *data_start, 14 | void *data_end, 15 | size_t item_size, 16 | sort_func_t sort_func, 17 | char *sort_func_name, 18 | cmp_func_t cmp_func 19 | ); 20 | 21 | #endif //SORT_ALGOS_C_SORT_TEST_H 22 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/bubble-sort-int-with-func.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | 9 | void bubble_sort(int a[], int len, cmp_int_func_t cmp_func) { 10 | for (int stop = len - 1; stop > 0; stop--) { 11 | for (int i = 0; i < stop; i++) { 12 | if ( !cmp_func(&a[i], &a[i+1])) { 13 | data_swap(a + i, a + i + 1, sizeof(int)); 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /include/sort-algos-int/selection-sort-int.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | void selection_sort(int a[], int len) { 8 | for (int left = 0; left < len; left++) { 9 | int min_p = left; 10 | for (int cur = left + 1; cur < len; cur++) { 11 | if ( a[cur] < a[min_p] ) { 12 | min_p = cur; 13 | } 14 | } 15 | int tmp = a[min_p]; 16 | a[min_p] = a[left]; 17 | a[left] = tmp; 18 | } 19 | } -------------------------------------------------------------------------------- /include/sorted-assert.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_SORTED_ASSERT_H 6 | #define SORT_ALGOS_C_SORTED_ASSERT_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | bool sorted_assert( 14 | const void *raw_data_start, const void *raw_data_end, 15 | const void *sorted_data_start, const void *sorted_data_end, 16 | size_t item_size, 17 | cmp_func_t cmp_func); 18 | 19 | #endif //SORT_ALGOS_C_SORTED_ASSERT_H 20 | -------------------------------------------------------------------------------- /include/sort-algos-int/sort-test.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/29. 3 | // 4 | 5 | #ifndef SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H 6 | #define SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | void sort_test( 15 | int a[], 16 | int len, 17 | void (*sort_func)(int [], int), 18 | char *sort_func_name, 19 | cmp_func_t cmp_func 20 | ); 21 | 22 | #endif //SORT_ALGOS_INT_WITH_FUNC_SORT_TEST_H 23 | -------------------------------------------------------------------------------- /include/sort-base.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by cybartist on 2020/8/10. 3 | // 4 | 5 | #ifndef SORT_ALGOS_C_SORT_BASE_H 6 | #define SORT_ALGOS_C_SORT_BASE_H 7 | 8 | #include 9 | #include 10 | 11 | typedef void (*sort_func_t)(void*, void*, size_t, bool (*)(const void*, const void*)); 12 | 13 | typedef void (*sort_int_func_t)(int[], int, bool (*)(const int*, const int*)); 14 | 15 | typedef bool (*cmp_func_t) (const void*, const void*); 16 | 17 | typedef bool (*cmp_int_func_t) (const int*, const int*); 18 | 19 | #endif //SORT_ALGOS_C_SORT_BASE_H 20 | -------------------------------------------------------------------------------- /include/time-utils.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #include 6 | 7 | struct timeval *create_start_point() { 8 | struct timeval *t = (struct timeval *) malloc(sizeof(struct timeval)); 9 | gettimeofday(t, NULL); 10 | return t; 11 | } 12 | 13 | uint64_t stop_watch_us(const struct timeval *t1) { 14 | struct timeval t2; 15 | gettimeofday(&t2, NULL); 16 | uint64_t timestamp1 = (uint64_t) t1->tv_sec * 1000000 + t1->tv_usec; 17 | uint64_t timestamp2 = (uint64_t) t2.tv_sec * 1000000 + t2.tv_usec; 18 | return timestamp2 - timestamp1; 19 | } 20 | -------------------------------------------------------------------------------- /include/sort-algos-int/insertion-sort-int.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | void insertion_sort(int a[], int len) { 8 | for (int left = 0; left < len; left++) { 9 | int min_p = left; 10 | for (int cur = min_p + 1; cur < len; cur++) { 11 | if ( a[cur] < a[min_p] ) { 12 | min_p = cur; 13 | } 14 | } 15 | int tmp = a[min_p]; 16 | for (int cursor = min_p - 1; cursor >= left; cursor--) { 17 | a[cursor + 1] = a[cursor]; 18 | } 19 | a[left] = tmp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/selection-sort-int-with-func.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | 9 | void selection_sort(int a[], int len, cmp_int_func_t cmp_func) { 10 | for (int left = 0; left < len; left++) { 11 | int min_p = left; 12 | for (int cur = left + 1; cur < len; cur++) { 13 | if ( cmp_func(&a[cur], &a[min_p]) ) { 14 | min_p = cur; 15 | } 16 | } 17 | int tmp = a[min_p]; 18 | a[min_p] = a[left]; 19 | a[left] = tmp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/insertion-sort-int-with-func.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | 9 | void insertion_sort(int a[], int len, cmp_int_func_t cmp_func) { 10 | for (int left = 0; left < len; left++) { 11 | int min_p = left; 12 | for (int cur = min_p + 1; cur < len; cur++) { 13 | if ( cmp_func(&a[cur], &a[min_p]) ) { 14 | min_p = cur; 15 | } 16 | } 17 | int tmp = a[min_p]; 18 | for (int cursor = min_p - 1; cursor >= left; cursor--) { 19 | a[cursor + 1] = a[cursor]; 20 | } 21 | a[left] = tmp; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sort Algos in C 2 | Sorting Algorithms General Implementations in C Programming Language 3 | 4 | 5 | 6 | - `sort-algos-int.c` is the most naive version. Every C programming language beginner could understand it. 7 | - `sort-algos-int-with-func.c` is the more advanced version based on `sort-algos-int.c`. In this sort of sorting algorithms, we implement the sorting algorithms with `cmp_func` to define the sorting rules. 8 | - `sort-algos.c` is the most general version to sort any kind of data type with any kind of sorting rules. For more information or usage of the most general version of sorting algorithms, you may find it in `sort-algos/sort-test.c` 9 | 10 | 11 | 12 | --- 13 | 14 | Tested Platform: 15 | 16 | - macOS 10.15 17 | - Ubuntu 18 | 19 | -------------------------------------------------------------------------------- /include/sort-algos-int/cocktail-sort-int.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | void cocktail_sort(int a[], int len) { 8 | int i, left = 0, right = len - 1; 9 | int temp; 10 | while (left < right) { 11 | for (i = left; i < right; i++) 12 | if (a[i] > a[i + 1]) { 13 | temp = a[i]; 14 | a[i] = a[i + 1]; 15 | a[i + 1] = temp; 16 | } 17 | right--; 18 | for (i = right; i > left; i--) 19 | if (a[i - 1] > a[i]) { 20 | temp = a[i]; 21 | a[i] = a[i - 1]; 22 | a[i - 1] = temp; 23 | } 24 | left++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /include/sort-algos/bubble-sort.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | void bubble_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func) { 11 | void *cursor_stop = (void *) data_end; 12 | cursor_stop -= item_size; 13 | 14 | while (data_start < cursor_stop) { 15 | void *cursor = (void *)data_start; 16 | while (cursor < cursor_stop) { 17 | if (!cmp_func(cursor, cursor + item_size)) { 18 | data_swap(cursor, cursor + item_size, item_size); 19 | } 20 | cursor += item_size; 21 | } 22 | cursor_stop -= item_size; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/cocktail-sort-int-with-func.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | void cocktail_sort(int a[], int len, cmp_int_func_t cmp_func) { 8 | int i, left = 0, right = len - 1; 9 | int temp; 10 | while (left < right) { 11 | for (i = left; i < right; i++) 12 | if (!cmp_func(&a[i], &a[i + 1])) { 13 | temp = a[i]; 14 | a[i] = a[i + 1]; 15 | a[i + 1] = temp; 16 | } 17 | right--; 18 | for (i = right; i > left; i--) 19 | if (!cmp_func(&a[i - 1], &a[i])) { 20 | temp = a[i]; 21 | a[i] = a[i - 1]; 22 | a[i - 1] = temp; 23 | } 24 | left++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /include/sort-algos/selection-sort.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | void selection_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func) { 11 | void *left = (void *) data_start; 12 | while (left < data_end) { 13 | void *min_p = left; 14 | void *cur = min_p + item_size; 15 | while (cur < data_end) { 16 | if (!cmp_func(min_p, cur)) { 17 | min_p = cur; 18 | } 19 | cur += item_size; 20 | } 21 | void *tmp = malloc(item_size); 22 | memcpy(tmp, left, item_size); 23 | memcpy(left, min_p, item_size); 24 | memcpy(min_p, tmp, item_size); 25 | free(tmp); 26 | left = left + item_size; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /include/sort-algos/cocktail-sort.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | void cocktail_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func) { 11 | void *i; 12 | void *left = data_start; 13 | void *right = data_end - item_size; 14 | while (left < right) { 15 | for (i = left; i < right; i += item_size) 16 | if ( !cmp_func(i, i + item_size) ) { 17 | data_swap(i, i + item_size, item_size); 18 | } 19 | right -= item_size; 20 | for (i = right; i > left; i -= item_size) 21 | if (!cmp_func(i - item_size, i)) { 22 | data_swap(i, i - item_size, item_size); 23 | } 24 | left += item_size; 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /include/sort-algos-int/quick-sort-int.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | void quick_sort(int a[], int len) { 10 | if (len <= 1) 11 | return; 12 | int mid = a[len - 1]; 13 | int left = 0; 14 | int right = len - 2; 15 | while (left < right) { 16 | while (a[left] <= mid && left < right) 17 | ++left; 18 | while ( a[right] > mid && left < right) 19 | --right; 20 | data_swap(a + left, a + right, sizeof(int)); 21 | } 22 | if ( a[len - 1] < a[left] ) { 23 | data_swap(a + left, a + len - 1, sizeof(int)); 24 | } else { 25 | ++left; 26 | data_swap(a + left, a + len - 1, sizeof(int)); 27 | } 28 | if (left) 29 | quick_sort(a, left); 30 | quick_sort(a + left + 1, len - left - 1); 31 | } -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/quick-sort-int-with-func.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | void quick_sort(int a[], int len, cmp_int_func_t cmp_func) { 11 | if (len <= 1) 12 | return; 13 | int mid = a[len - 1]; 14 | int left = 0; 15 | int right = len - 2; 16 | while (left < right) { 17 | while (cmp_func(&a[left], &mid) && left < right) 18 | ++left; 19 | while (!cmp_func(&a[right], &mid) && left < right) 20 | --right; 21 | data_swap(a + left, a + right, sizeof(int)); 22 | } 23 | if ( cmp_func(&a[len-1], &a[left]) ) { 24 | data_swap(a + left, a + len - 1, sizeof(int)); 25 | } else { 26 | ++left; 27 | data_swap(a + left, a + len - 1, sizeof(int)); 28 | } 29 | if (left) 30 | quick_sort(a, left, cmp_func); 31 | quick_sort(a + left + 1, len - left - 1, cmp_func); 32 | } -------------------------------------------------------------------------------- /include/sort-algos/insertion-sort.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | void insertion_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func) { 12 | void *left = (void *) data_start; 13 | while (left < data_end) { 14 | void *min_p = left; 15 | void *cur = min_p + item_size; 16 | while (cur < data_end) { 17 | if (!cmp_func(min_p, cur)) { 18 | min_p = cur; 19 | } 20 | cur += item_size; 21 | } 22 | void *tmp = malloc(item_size); 23 | memcpy(tmp, min_p, item_size); 24 | 25 | void *cursor = min_p; 26 | cursor -= item_size; 27 | while (left <= cursor) { 28 | memcpy(cursor + item_size, cursor, item_size); 29 | cursor -= item_size; 30 | } 31 | 32 | memcpy(left, tmp, item_size); 33 | 34 | free(tmp); 35 | left = left + item_size; 36 | } 37 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 ismdeep 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /include/sort-algos/quick-sort.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | 11 | void quick_sort(void *data_start, void *data_end, size_t item_size, cmp_func_t cmp_func) { 12 | if (data_start >= data_end - item_size) 13 | return; 14 | void *mid = data_end - item_size; 15 | void *left = data_start; 16 | void *right = data_end - item_size - item_size; 17 | 18 | while (left < right) { 19 | while ( cmp_func(left, mid) && left < right) 20 | left += item_size; 21 | while (! cmp_func(right, mid) && left < right) 22 | right -= item_size; 23 | data_swap(left, right, item_size); 24 | } 25 | if ( cmp_func(data_end - item_size, left)) { 26 | data_swap(left, data_end - item_size, item_size); 27 | } else { 28 | left += item_size; 29 | data_swap(left, data_end - item_size, item_size); 30 | } 31 | if (left != data_start) 32 | quick_sort(data_start, left, item_size, cmp_func); 33 | quick_sort(left + item_size, data_end, item_size, cmp_func); 34 | } 35 | -------------------------------------------------------------------------------- /include/sort-algos-int/sort-test.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/29. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | void sort_test( 15 | int data[], 16 | int len, 17 | void (*sort_func)(int [], int), 18 | char *sort_func_name, 19 | cmp_func_t cmp_func 20 | ) { 21 | /* Copy data from raw to selection_sort_data */ 22 | int *sort_data = (int *) malloc(sizeof(int) * len ); 23 | memcpy(sort_data, data, sizeof(int) * len); 24 | 25 | /* Sort array **sort_data** with **sort_func()** algorithm */ 26 | struct timeval *start_point = create_start_point(); 27 | sort_func(sort_data, len); 28 | printf("Time elapse[%s]: %.2lf ms\n", sort_func_name, stop_watch_us(start_point) / 1000.0); 29 | 30 | /* Assert sort result */ 31 | if (sorted_assert(data, data + len, sort_data, sort_data + len, sizeof(int), cmp_func)) { 32 | printf("Successfully.\n\n"); 33 | } else { 34 | printf("Unsuccessfully.\n\n"); 35 | } 36 | 37 | free(sort_data); 38 | free(start_point); 39 | } 40 | -------------------------------------------------------------------------------- /sort-algos-int.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define ARR_SIZE 20000 15 | 16 | bool cmp(const int *a, const int *b) { 17 | return *a <= *b; 18 | } 19 | 20 | int main() { 21 | srandom((unsigned) time(NULL)); 22 | 23 | /* Generate raw_data[ARR_SIZE] */ 24 | int raw_data[ARR_SIZE]; 25 | for (int i = 0; i < ARR_SIZE; i++) { 26 | raw_data[i] = (int) (random() % 1000); 27 | } 28 | 29 | sort_test(raw_data, ARR_SIZE, selection_sort, "selection_sort", (cmp_func_t) cmp); 30 | sort_test(raw_data, ARR_SIZE, bubble_sort , "bubble_sort" , (cmp_func_t) cmp); 31 | sort_test(raw_data, ARR_SIZE, cocktail_sort , "cocktail_sort" , (cmp_func_t) cmp); 32 | sort_test(raw_data, ARR_SIZE, insertion_sort, "insertion_sort", (cmp_func_t) cmp); 33 | sort_test(raw_data, ARR_SIZE, quick_sort , "quick_sort" , (cmp_func_t) cmp); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /include/sort-algos-int-with-func/sort-test.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/30. 3 | // 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | void sort_test( 16 | int data[], 17 | int len, 18 | sort_int_func_t sort_func, 19 | char *sort_func_name, 20 | cmp_int_func_t cmp_func 21 | ) { 22 | /* Copy data from raw to selection_sort_data */ 23 | int *sort_data = (int *) malloc(sizeof(int) * len ); 24 | memcpy(sort_data, data, sizeof(int) * len); 25 | 26 | /* Sort array **sort_data** with **sort_func()** algorithm */ 27 | struct timeval *start_point = create_start_point(); 28 | sort_func(sort_data, len, cmp_func); 29 | printf("Time elapse[%s]: %.2lf ms\n", sort_func_name, stop_watch_us(start_point) / 1000.0); 30 | 31 | /* Assert sort result */ 32 | if (sorted_assert(data, data + len, sort_data, sort_data + len, sizeof(int), 33 | (bool (*)(const void *, const void *)) cmp_func)) { 34 | printf("Successfully.\n\n"); 35 | } else { 36 | printf("Unsuccessfully.\n\n"); 37 | } 38 | 39 | free(sort_data); 40 | free(start_point); 41 | } 42 | -------------------------------------------------------------------------------- /sort-algos-int-with-func.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/28. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | #define ARR_SIZE 20000 18 | 19 | bool cmp(const int *a, const int *b) { 20 | return *a >= *b; 21 | } 22 | 23 | int main() { 24 | srandom((unsigned) time(NULL)); 25 | 26 | /* Generate raw_data[ARR_SIZE] */ 27 | int raw_data[ARR_SIZE]; 28 | for (int i = 0; i < ARR_SIZE; i++) { 29 | raw_data[i] = (int) (random() % 1000); 30 | } 31 | 32 | sort_test(raw_data, ARR_SIZE, selection_sort, "selection_sort", cmp); 33 | sort_test(raw_data, ARR_SIZE, bubble_sort , "bubble_sort" , cmp); 34 | sort_test(raw_data, ARR_SIZE, cocktail_sort , "cocktail_sort" , cmp); 35 | sort_test(raw_data, ARR_SIZE, insertion_sort, "insertion_sort", cmp); 36 | sort_test(raw_data, ARR_SIZE, quick_sort , "quick_sort" , cmp); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /include/sort-algos/sort-test.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/27. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | void sort_test( 15 | void *data_start, 16 | void *data_end, 17 | size_t item_size, 18 | sort_func_t sort_func, 19 | char *sort_func_name, 20 | cmp_func_t cmp_func 21 | ) { 22 | 23 | /* Copy data from raw to selection_sort_data */ 24 | void *sort_data = (void *) malloc( data_end - data_start ); 25 | memcpy(sort_data, data_start, data_end - data_start); 26 | 27 | /* Sort array **sort_data** with **sort_func()** algorithm */ 28 | struct timeval *start_point = create_start_point(); 29 | sort_func(sort_data, sort_data + (data_end - data_start), item_size, cmp_func); 30 | printf("Time elapse[%s]: %.2lf ms\n", sort_func_name, stop_watch_us(start_point) / 1000.0); 31 | 32 | /* Assert sort result */ 33 | if (sorted_assert(data_start, data_end, sort_data, sort_data + (data_end - data_start), item_size, cmp_func)) { 34 | printf("Successfully.\n\n"); 35 | } else { 36 | printf("Unsuccessfully.\n\n"); 37 | } 38 | 39 | free(sort_data); 40 | free(start_point); 41 | } 42 | -------------------------------------------------------------------------------- /sort-algos.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #define ARR_SIZE 20000 17 | 18 | bool cmp(const double *data_left, const double *data_right) { 19 | return *data_left <= *data_right; 20 | } 21 | 22 | int main() { 23 | srandom((unsigned) time(NULL)); 24 | 25 | /* Generate raw_data[ARR_SIZE] */ 26 | double raw_data[ARR_SIZE]; 27 | for (int i = 0; i < ARR_SIZE; i++) { 28 | raw_data[i] = (double) (random() % 1000); 29 | } 30 | 31 | /* Testing sort functions */ 32 | sort_test(raw_data, raw_data + ARR_SIZE, sizeof(double), selection_sort, "selection_sort", (cmp_func_t) cmp); 33 | sort_test(raw_data, raw_data + ARR_SIZE, sizeof(double), bubble_sort, "bubble_sort", (cmp_func_t) cmp); 34 | sort_test(raw_data, raw_data + ARR_SIZE, sizeof(double), cocktail_sort, "cocktail_sort", (cmp_func_t) cmp); 35 | sort_test(raw_data, raw_data + ARR_SIZE, sizeof(double), insertion_sort, "insertion_sort", (cmp_func_t) cmp); 36 | sort_test(raw_data, raw_data + ARR_SIZE, sizeof(double), quick_sort, "quick_sort", (cmp_func_t) cmp); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(sort-algos C) 3 | project(sort-algos-int C) 4 | project(sort-algos-int-with-func C) 5 | 6 | set(CMAKE_C_STANDARD 11) 7 | 8 | include_directories("./include") 9 | 10 | add_executable(sort-algos 11 | 12 | include/data-swap.c 13 | include/sort-algos/selection-sort.c 14 | include/sort-algos/bubble-sort.c 15 | include/sort-algos/insertion-sort.c 16 | include/sort-algos/quick-sort.c 17 | include/sort-algos/cocktail-sort.c 18 | include/sorted-assert.c 19 | include/sort-algos/sort-test.c 20 | include/time-utils.c 21 | include/sort-base.h 22 | sort-algos.c) 23 | 24 | add_executable(sort-algos-int 25 | 26 | include/sort-algos-int/selection-sort-int.c 27 | include/sort-algos-int/bubble-sort-int.c 28 | include/sort-algos-int/insertion-sort-int.c 29 | include/sort-algos-int/quick-sort-int.c 30 | include/sort-algos-int/cocktail-sort-int.c 31 | include/sorted-assert.c 32 | include/data-swap.c 33 | include/sort-algos-int/sort-test.c 34 | include/time-utils.c 35 | include/sort-base.h 36 | sort-algos-int.c) 37 | 38 | add_executable(sort-algos-int-with-func 39 | 40 | include/sort-algos-int-with-func/selection-sort-int-with-func.c 41 | include/sort-algos-int-with-func/bubble-sort-int-with-func.c 42 | include/sort-algos-int-with-func/insertion-sort-int-with-func.c 43 | include/sort-algos-int-with-func/quick-sort-int-with-func.c 44 | include/sort-algos-int-with-func/cocktail-sort-int-with-func.c 45 | include/sorted-assert.c 46 | include/data-swap.c 47 | include/sort-algos-int-with-func/sort-test.c 48 | include/time-utils.c 49 | include/sort-base.h 50 | sort-algos-int-with-func.c) 51 | -------------------------------------------------------------------------------- /include/sorted-assert.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by ismdeep on 2020/1/26. 3 | // 4 | 5 | #include 6 | #include 7 | 8 | bool data_equal(const void *data1, const void *data2, size_t item_size) { 9 | for (size_t i = 0; i < item_size; i++) { 10 | if ( *((unsigned char *)data1 + i) != *((unsigned char *)data2 + i) ) { 11 | return false; 12 | } 13 | } 14 | return true; 15 | } 16 | 17 | bool sorted_assert( 18 | const void *raw_data_start, const void *raw_data_end, 19 | const void *sorted_data_start, const void *sorted_data_end, 20 | size_t item_size, 21 | cmp_func_t cmp_func) { 22 | 23 | size_t count = (raw_data_end - raw_data_start) / item_size; 24 | bool *used = (bool *) malloc(sizeof(bool) * count); 25 | void *cursor; 26 | 27 | /* Check if all raw_data are the same with sorted_data (no data loose, no data append) */ 28 | for (int i = 0; i < count; i++) { 29 | used[i] = false; 30 | } 31 | cursor = (void *) sorted_data_start; 32 | while (cursor < sorted_data_end) { 33 | bool found = false; 34 | for (int i = 0; i < count; i++) { 35 | if (!used[i] && data_equal(cursor, raw_data_start + i * item_size, item_size)) { 36 | used[i] = true; 37 | found = true; 38 | break; 39 | } 40 | } 41 | if (!found) { 42 | return false; 43 | } 44 | cursor += item_size; 45 | } 46 | 47 | /* Check if every pair of adjacent is satisfied **cmp_func()** */ 48 | cursor = (void *)sorted_data_start; 49 | cursor += item_size; 50 | while (cursor < sorted_data_end) { 51 | if (!cmp_func(cursor - item_size, cursor)) { 52 | return false; 53 | } 54 | cursor += item_size; 55 | } 56 | 57 | free(used); 58 | return true; 59 | } 60 | --------------------------------------------------------------------------------