Advertisement
csansoon

P10.01 P81966 Dichotomic search

Dec 23rd, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int position(double x, const vector<double>& v, int left, int right) {
  7.                 if (left > right) return -1;
  8.                 int mid = (left+right)/2;
  9.                 if (x < v[mid]) return position(x, v, left, mid-1);
  10.                 if (x > v[mid]) return position(x, v, mid+1, right);
  11.                 return mid;
  12. }
  13.  
  14. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement