Add dichotomic search algorithm in sorted tab C implementation.
[Algorithmic_C.git] / TP1 / exo3 / exo3.c
CommitLineData
0d5ad0cf
JB
1#include <stdio.h>
2
3int rechercheDicho(int e, int T[], int d, int f) {
4 int m;
5 if (f < d) {return -1;}
6 m = (f+d)/2;
7 if (T[m] == e) {return m;}
8 if (e < T[m]) {
9 return rechercheDicho(e, T, d, m-1);
10 } else {
11 return rechercheDicho(e, T, m+1, f);
12 }
13}
14
15int main() {
16 int T[9] = {1, 2, 3, 6, 6, 7, 9, 11, 12};
17
18 int result = rechercheDicho(8, T, 0, 9);
19
20 if (result > 0) {
21 printf("element present a index=%d\n", result);
22 } else {
23 printf("element non present\n");
24 }
25
26 return 0;
27}