| 1 | #include "utils.h" |
| 2 | #include "sort.h" |
| 3 | |
| 4 | bool ascending(int a, int b) { |
| 5 | return a > b; |
| 6 | } |
| 7 | |
| 8 | bool descending(int a, int b) { |
| 9 | return a < b; |
| 10 | } |
| 11 | |
| 12 | static bool sort_first(int* array, int length, criteria_cb criteria) { |
| 13 | bool rt = false; |
| 14 | for (int i = 0; i < length-1; i++) { |
| 15 | if (criteria(array[i], array[i+1])) { |
| 16 | swap_int(&array[i], &array[i+1]); |
| 17 | if (!rt) { rt = true; }; |
| 18 | } |
| 19 | } |
| 20 | return rt; |
| 21 | } |
| 22 | |
| 23 | /* this function is awaited in the array.c file */ |
| 24 | void sort_array(int* array, int length, criteria_cb criteria) { |
| 25 | bool rt; |
| 26 | do { |
| 27 | rt = sort_first(array, length, criteria); |
| 28 | } while (rt); |
| 29 | } |