TechGeek

BinarySearch-Recursive

Jul 27th, 2018
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static int binarySearch(int[] a, int target) {
  2.     return binarySearch(a, 0, a.length-1, target);
  3. }
  4.  
  5. public static int binarySearch(int[] a, int start, int end, int target) {
  6.     int middle = (start + end) / 2;
  7.     if(end < start) {
  8.         return -1;
  9.     }
  10.  
  11.     if(target==a[middle]) {
  12.         return middle;
  13.     } else if(target<a[middle]) {
  14.         return binarySearch(a, start, middle - 1, target);
  15.     } else {
  16.         return binarySearch(a, middle + 1, end, target);
  17.     }
  18. }
RAW Paste Data