Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. private int bsearch(int[] a, int x) {
  2. int low = 0;
  3. int high = a.length - 1;
  4. int mid;
  5.  
  6. if (a[0] < x) {
  7. return 0;
  8. }
  9.  
  10. while (low <= high) {
  11. mid = (low + high) / 2;
  12.  
  13. if (a[mid] > x)
  14. low = mid + 1;
  15. else if (a[mid] < x)
  16. high = mid - 1;
  17. else
  18. return mid + 1;
  19. }
  20.  
  21. return -1;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement