Recursive Binary Search Execution
What output does the binary search method produce when searching for 8?
public class BinarySearchRec {
public static int binarySearch(int[] arr, int low, int high, int target) {
if(low > high) return -1;
int mid = (low + high) / 2;
if(arr[mid] == target) return mid;
else if(arr[mid] > target)
return binarySearch(arr, low, mid - 1, target);
else
return binarySearch(arr, mid + 1, high, target);
}
public static void main(String[] args) {
int[] sorted = {2, 4, 6, 8, 10, 12};
System.out.println(binarySearch(sorted, 0, sorted.length - 1, 8));
}
}
A
3
B
4
C
-1
D
2
APFIVE