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
-1
B
3
C
2
D
4
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | mtanarupi | 2 | 2 | 0m 00s | 200 |
| #2 | ngvandangthanh | 1 | 1 | 0m 00s | 100 |
| #3 | bommasam000 | 2 | 4 | 2m 35s | 25 |
| #4 | y.seong2027 | 1 | 2 | 2m 38s | -68 |
| #5 | geethasailaja | 0 | 1 | 2m 28s | -158 |
| #6 | singhris000 | 1 | 2 | 3h 07m | -11,163 |
APFIVE