Recursive Binary Search Trace
Consider the following recursive binary search method:
public static boolean contains(int[] data, int value, int low, int high) {
if (low > high) {
return false;
}
int mid = (low + high) / 2;
if (data[mid] == value) {
return true;
} else if (data[mid] > value) {
return contains(data, value, low, mid - 1);
} else {
return contains(data, value, mid + 1, high);
}
}
Suppose an array data is defined as {5, 12, 23, 34, 45, 56, 67, 78, 89, 90}. If the method is invoked by the call contains(data, 67, 0, 9), what is the sequence of values assigned to the mid variable across all recursive calls?
A
4, 6, 7, 5
B
4, 8, 5, 6
C
5, 7, 6, 4
D
4, 7, 5, 6
Question Leaderboard
Not enough data yet to show leaderboard.
