X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP_13%2Fexo1%2Flib%2Fsort.c;h=f8917ea5ab6987bb96ccc9909f5bbdfa6f0d1cb8;hb=cfdd46d2e85b05f77a03ae31f721e2fd4030996f;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hpb=eddc018ded5c49cd7301031d7c6c80022fb91f9d;p=TD_C.git diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c index e69de29..f8917ea 100644 --- a/TP_13/exo1/lib/sort.c +++ b/TP_13/exo1/lib/sort.c @@ -0,0 +1,39 @@ +#include "utils.h" +#include "sort.h" + +bool ascending(int a, int b) { + return a > b; +} + +bool descending(int a, int b) { + return a < b; +} + +bool ascending_and_even(int a, int b) { + return (((a % 2 != 0) && (b % 2 == 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \ + || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); +} + +bool ascending_and_odd(int a, int b) { + return (((a % 2 == 0) && (b % 2 != 0)) || ((a % 2 == 0) && (b % 2 == 0) && ascending(a, b)) \ + || ((a % 2 != 0) && (b % 2 != 0) && ascending(a, b))); +} + +static bool sort_first(int* array, unsigned length, criteria_cb criteria) { + bool rt = false; + for (unsigned i = 0; i < length-1; i++) { + if (criteria(array[i], array[i+1])) { + swap_int(&array[i], &array[i+1]); + rt = true; + } + } + return rt; +} + +/* this function is awaited in the array.c file */ +void sort_bubble_array(int* array, unsigned length, criteria_cb criteria) { + bool rt; + do { + rt = sort_first(array, length, criteria); + } while (rt); +}