Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. bool binary_search(const int *a, int n, int v)
  2. {
  3. int low = 0;
  4. int high = n;
  5. int mid;
  6. int found = 0;
  7.  
  8. while(low <= high && found == 0)
  9. {
  10. mid = (high + low)/2;
  11. if(a[mid] == v)
  12. found = 1;
  13. else if(a[mid] > v)
  14. high = mid - 1;
  15. else
  16. low = mid + 1;
  17. }
  18.  
  19. if(found == 1)
  20. return true;
  21. else
  22. return false;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement