Move callback functions into the same file
[TD_C.git] / TP_13 / exo1 / lib / sort.c
CommitLineData
33b9c646
JB
1#include "utils.h"
2#include "sort.h"
3
0b8ccced
JB
4bool is_even(int a) {
5 return (a % 2 == 0);
6}
7
8bool is_odd(int a) {
9 return (a % 2 != 0);
10
11}
12
33b9c646
JB
13bool ascending(int a, int b) {
14 return a > b;
15}
16
17bool descending(int a, int b) {
18 return a < b;
19}
20
e4001676 21bool ascending_and_even(int a, int b) {
cfdd46d2
JB
22 return (((a % 2 != 0) && (b % 2 == 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \
23 || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b)));
e4001676
JB
24}
25
26bool ascending_and_odd(int a, int b) {
cfdd46d2
JB
27 return (((a % 2 == 0) && (b % 2 != 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \
28 || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b)));
e4001676
JB
29}
30
210f7f05 31static bool sort_first(int* array, unsigned length, s_criteria_cb sort_criteria) {
33b9c646 32 bool rt = false;
e4001676 33 for (unsigned i = 0; i < length-1; i++) {
210f7f05 34 if (sort_criteria(array[i], array[i+1])) {
0139cafd 35 swap_int(&array[i], &array[i+1]);
c944d83b 36 rt = true;
33b9c646
JB
37 }
38 }
39 return rt;
40}
41
475ee86d 42/* the feature of this function is awaited in the array.c file */
210f7f05 43void sort_bubble_array(int* array, unsigned length, s_criteria_cb sort_criteria) {
33b9c646
JB
44 bool rt;
45 do {
210f7f05 46 rt = sort_first(array, length, sort_criteria);
33b9c646
JB
47 } while (rt);
48}