Let A be a list of N integers in increasing order, N > 0. There are no
duplicates. Consider the following algorithm for binary search (div means
integer division - i.e. discard the fractional remainder when you divide).
This algorithm outputs the index of the target if it is in the list and
-1 if it is not.
1. Set low = 0.
2. Set high = N-1.
3. Set mid = (low + high) div 2.
4. While low ≤ high and target ≠ A[mid] do the following:
a. If target < A[mid], set high = mid-1; otherwise, set low = mid+1.
b. Set mid = (low + high) div 2.
5. If low ≤ high, output mid.
Otherwise, output -1.
-
Trace this algorithm for the following list (N=9) and a target value of
73, showing the value of low, high, and mid immediately before the loop
starts and after each iteration of the loop. What is the final value that
is output?
0 1 2 3 4 5 6 7 8
--------------------------------------------
| 13 | 29 | 33 | 40 | 58 | 64 | 68 | 73 | 89 |
--------------------------------------------
-
Repeat part (a) for a target value of 36.
-
If you had 1 billion data values in increasing order and you had to
search for a specific target using binary search, approxmiately how many
data values do you need to look at in order to determine if the target is
present or not? Explain your answer.