70a766b505773ba6e14823aef9b194c9c55c0b95
[TD_C.git] / TP_13 / exo1 / lib / sort.c
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 bool ascending_and_even(int a, int b) {
13 return (ascending(a, b) && (a % 2 == 0));
14 }
15
16 bool ascending_and_odd(int a, int b) {
17 return (ascending(a, b) && (a % 2 != 0));
18 }
19
20 static bool sort_first(int* array, unsigned length, criteria_cb criteria) {
21 bool rt = false;
22 for (unsigned i = 0; i < length-1; i++) {
23 if (criteria(array[i], array[i+1])) {
24 swap_int(&array[i], &array[i+1]);
25 rt = true;
26 }
27 }
28 return rt;
29 }
30
31 /* this function is awaited in the array.c file */
32 void sort_bubble_array(int* array, unsigned length, criteria_cb criteria) {
33 bool rt;
34 do {
35 rt = sort_first(array, length, criteria);
36 } while (rt);
37 }