@@ -1,12 +1,12 @@
-// exemple de dichotomie
-void dicho() {
+// example of binary search
+int dicho(int pos, int size) {
int l = 0, r = size-1;
while(l < r) {
int m = l+(r-l)/2;
- if(position <= m)
+ if(pos <= m) // replace with predicate
r = m;
else
l = m+1;
}
- // position : l
-}
+ return l;
+}
@@ -0,0 +1,11 @@
+#include <bits/stdc++.h>
+using namespace std;
+
+#include "Dichotomie.cpp"
+int main() {
+ for(int s = 0; s < 100; s++)
+ for(int i = 0; i < s; i++)
+ assert(dicho(i, s) == i);
+ return 0;