Binary Search - Find Element in Sorted Finite Array
package org.wesome.dsalgo;
public class BinarySearch {
public static int binarySearch(int[] arr, int start, int end, int find) {
if (start > end) {
return -1;
}
int mid = start + (end - start) / 2;
if (arr[mid] == find) {
return mid;
}
// If the element is present at the middle itself
if (arr[mid] >= find) {
// If element is smaller than mid, then it can only be present in left subarray
return binarySearch(arr, start, mid - 1, find);
}
// Else the element can only be present in right subarray
else return binarySearch(arr, mid + 1, end, find);
}
}