Suppose that that we have an array called list initialized as follows: int[] lis
ID: 3643651 • Letter: S
Question
Suppose that that we have an array called list initialized as follows:int[] list = {-2, 8, 13, 22, 25, 25, 38, 42, 51, 103};
This would construct the following array:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
+----+----+----+----+----+----+----+----+----+-----+
| -2 | 8 | 13 | 22 | 25 | 25 | 38 | 42 | 51 | 103 |
+----+----+----+----+----+----+----+----+----+-----+
Note that the method calls below is of the form:
binarySearch(int[] a, int low, int high, int target)
Write the complete program and
a) What values would low, high and mid take on for the following call:
binarySearch(list, 103, 0, 9, 103)
and what value would be returned?
b) What values would low, high and mid take on for the following call:
binarySearch(list, 2, 8, 30)
and what value would be returned?
Explanation / Answer
low=0
high=9
mid=(9+0)/2=4
The returned value depends on the implementation.
The returned value can be 9, that is the index of 103 in this case.
Or it can be returned TRUE as notification that the element is found in the array.