Add dichotomic search algorithm in sorted tab C implementation.
[Algorithmic_C.git] / TP1 / exo3 / exo3.c
diff --git a/TP1/exo3/exo3.c b/TP1/exo3/exo3.c
new file mode 100644 (file)
index 0000000..f668493
--- /dev/null
@@ -0,0 +1,27 @@
+#include <stdio.h>
+
+int rechercheDicho(int e, int T[], int d, int f) {  
+    int m;
+    if (f < d) {return -1;}
+    m = (f+d)/2;
+    if (T[m] == e) {return m;}
+    if (e < T[m]) {
+        return rechercheDicho(e, T, d, m-1);
+    } else {
+        return rechercheDicho(e, T, m+1, f);
+    }
+}
+
+int main() {
+    int T[9] = {1, 2, 3, 6, 6, 7, 9, 11, 12};
+
+    int result = rechercheDicho(8, T, 0, 9);
+
+    if (result > 0) { 
+        printf("element present a index=%d\n", result);
+    } else {
+        printf("element non present\n");
+    }
+
+    return 0;
+}