Advertisement
JosepRivaille

P33412: Resistant dichotomic search

Mar 21st, 2016
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. bool resistant_search(double x, const vector<double>& v)
  7. {
  8.   if (v.size() == 0) return false;
  9.   int l = 0, r = v.size() - 1, mid;
  10.   while (l + 1 < r) {
  11.     mid = (l+r)/2;
  12.     if (v[mid] == x || v[mid-1] == x || v[mid+1] == x) return true;
  13.     else if (v[mid] > x) r = mid;
  14.     else l = mid;
  15.   }
  16.   return ((x == v[l]) || (x == v[r]));
  17. }
  18.  
  19. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement