Commit | Line | Data |
---|---|---|
33b9c646 JB |
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 | ||
e4001676 | 12 | bool ascending_and_even(int a, int b) { |
cfdd46d2 JB |
13 | return (((a % 2 != 0) && (b % 2 == 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \ |
14 | || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); | |
e4001676 JB |
15 | } |
16 | ||
17 | bool ascending_and_odd(int a, int b) { | |
cfdd46d2 JB |
18 | return (((a % 2 == 0) && (b % 2 != 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \ |
19 | || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); | |
e4001676 JB |
20 | } |
21 | ||
210f7f05 | 22 | static bool sort_first(int* array, unsigned length, s_criteria_cb sort_criteria) { |
33b9c646 | 23 | bool rt = false; |
e4001676 | 24 | for (unsigned i = 0; i < length-1; i++) { |
210f7f05 | 25 | if (sort_criteria(array[i], array[i+1])) { |
0139cafd | 26 | swap_int(&array[i], &array[i+1]); |
c944d83b | 27 | rt = true; |
33b9c646 JB |
28 | } |
29 | } | |
30 | return rt; | |
31 | } | |
32 | ||
475ee86d | 33 | /* the feature of this function is awaited in the array.c file */ |
210f7f05 | 34 | void sort_bubble_array(int* array, unsigned length, s_criteria_cb sort_criteria) { |
33b9c646 JB |
35 | bool rt; |
36 | do { | |
210f7f05 | 37 | rt = sort_first(array, length, sort_criteria); |
33b9c646 JB |
38 | } while (rt); |
39 | } |