diff --git a/td2/td2.md b/td2/td2.md new file mode 100644 index 0000000..0380eec --- /dev/null +++ b/td2/td2.md @@ -0,0 +1,38 @@ +# TD2 + +## Algorythme dichotomique en C + +```c +int f(int t[], int target, int len) { + int min = 0; + int max = len-1; + while (min < max) { + int i = (min+max)/2 + int val = t[i]; + if (target < val) { + max = i; + } else if (target > val) { + min = i; + } else { + min = i; + max = i; + } + } + return min; +} +``` + +Preuve de terminaison : + +- **variant :** $\lfloor log_2(\frac{max-min}{2}) \rfloor$ + +Preuve de correction : + +- **invariant :** $min \leq i_{target} \leq max$ +- **après la boucle :** $min = max$ + + donc $min \leq i_{target} \leq min$ + + donc $i_{target} = min$ + +Complexité : $O(log_2(n))$ diff --git a/td2/td2.pdf b/td2/td2.pdf new file mode 100644 index 0000000..bb3194b Binary files /dev/null and b/td2/td2.pdf differ