Binary Search Hacks
Binary search hacks
public class BinarySearch {
public static int index = 0;
public static void binarySearch(int arr[], int low, int high, int search) {
int mid = (low + high) / 2;
if (arr[mid] < search) {
binarySearch(arr, mid + 1, high, search);
} else if (arr[mid] > search) {
binarySearch(arr, low, mid - 1, search);
} else {
index = mid;
}
}
public static void main(String[] args) {
int[] array = {1, 3, 5, 7, 9, 23, 45, 67};
binarySearch(array, 0, array.length - 1, 45);
System.out.println(index);
}
}
BinarySearch.main(null)