(int[] a, int x)
| 3 | public class BinarySearch { |
| 4 | |
| 5 | public static int binarySearch(int[] a, int x) { |
| 6 | int low = 0; |
| 7 | int high = a.length - 1; |
| 8 | int mid; |
| 9 | |
| 10 | while (low <= high) { |
| 11 | mid = (low + high) / 2; |
| 12 | if (a[mid] < x) { |
| 13 | low = mid + 1; |
| 14 | } else if (a[mid] > x) { |
| 15 | high = mid - 1; |
| 16 | } else { |
| 17 | return mid; |
| 18 | } |
| 19 | } |
| 20 | return -1; |
| 21 | } |
| 22 | |
| 23 | public static int binarySearchRecursive(int[] a, int x, int low, int high) { |
| 24 | if (low > high) return -1; // Error |