Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. public class Searchh {
  2. public static final int NOT_FOUND = -1;
  3.  
  4. public static int binarySearch(Integer[] a, int x) {
  5. int low = 0;
  6. int high = a.length - 1;
  7. int mid;
  8. while (low <= high) {
  9. mid = (low + high) / 2;
  10.  
  11. if (a[mid].compareTo(x) < 0)
  12. low = mid + 1;
  13. else if (a[mid].compareTo(x) > 0)
  14. high = mid - 1;
  15. else
  16. return mid;
  17. }
  18. return NOT_FOUND;
  19. }
  20.  
  21. public static void main(String[] args) {
  22. int SIZE = 6;
  23. Integer[] a = { -3, 10, 5, 24, 45 };
  24. System.out.println("45.3 found at " + binarySearch(a, 45));
  25. }
  26. }
Add Comment
Please, Sign In to add comment