Move callback functions into the same file
[TD_C.git] / TP_13 / exo1 / lib / sort.c
index aef713d8a69db4c9df3dddfda882d349ae06846b..0bd005ed639c0921fcc0ecedab050a2df352137d 100644 (file)
@@ -1,6 +1,15 @@
 #include "utils.h"
 #include "sort.h"
 
+bool is_even(int a) {
+    return (a % 2 == 0);
+}
+
+bool is_odd(int a) {
+    return (a % 2 != 0);
+
+}
+
 bool ascending(int a, int b) {
     return a > b;
 }
@@ -9,21 +18,31 @@ bool descending(int a, int b) {
     return a < b;
 }
 
-static bool sort_first(int* array, int length, criteria_cb criteria) {
+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, s_criteria_cb sort_criteria) {
     bool rt = false;
-    for (int i = 0; i < length-1; i++) {
-        if (criteria(array[i], array[i+1])) {
-            swap_int_ptr(&array[i], &array[i+1]);
-            if (!rt) { rt = true; };
+    for (unsigned i = 0; i < length-1; i++) {
+        if (sort_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_array(int* array, int length, criteria_cb criteria) {
+/* the feature of this function is awaited in the array.c file */
+void sort_bubble_array(int* array, unsigned length, s_criteria_cb sort_criteria) {
     bool rt;
     do {
-        rt = sort_first(array, length, criteria);
+        rt = sort_first(array, length, sort_criteria);
    } while (rt);
 }