Searching a sorted array
1.[2p] How many comparisons does binary search need in the worst case on an array of elements?
How many comparisons does binary search need in the worst case on an array of elements?
2.[2p] What invariant does binary search maintain?
What invariant does binary search maintain?
The answer is: If the target is present at all, its index lies between lo and hi inclusive
The answer is: If the target is present at all, its index lies between lo and hi inclusive
The answer is: If the target is present at all, its index lies between lo and hi inclusive
3.[3p] Trace binary search for 13 in the array 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47. How many comparisons does it make?
Trace binary search for 13 in the array 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47. How many comparisons does it make?
4.[3p] The decision-tree lower bound for comparison-based search on elements is because
The decision-tree lower bound for comparison-based search on elements is because
The answer is: the algorithm needs at least distinct leaves, and a binary tree of height has at most
The answer is: the algorithm needs at least distinct leaves, and a binary tree of height has at most
The answer is: the algorithm needs at least distinct leaves, and a binary tree of height has at most
5.[2p] Interpolation search can beat because
Interpolation search can beat because
The answer is: it uses the value of the key to guess a position, so the comparison lower bound does not apply to it
The answer is: it uses the value of the key to guess a position, so the comparison lower bound does not apply to it
The answer is: it uses the value of the key to guess a position, so the comparison lower bound does not apply to it
6.[3p] Why is preferred to ?
Why is preferred to ?
The answer is: The sum can overflow a fixed-width integer, which shipped as a bug in Java's Arrays.binarySearch for nine years
The answer is: The sum can overflow a fixed-width integer, which shipped as a bug in Java's Arrays.binarySearch for nine years
The answer is: The sum can overflow a fixed-width integer, which shipped as a bug in Java's Arrays.binarySearch for nine years
7.[2p] In the array 2, 4, 4, 4, 7, 9, lower bound for 4 returns 1 and upper bound returns 4. What does the difference between them count?
In the array 2, 4, 4, 4, 7, 9, lower bound for 4 returns 1 and upper bound returns 4. What does the difference between them count?
8.[2p] Binary search on an array that is not sorted reports an error rather than a wrong answer.
Binary search on an array that is not sorted reports an error rather than a wrong answer.
The answer is: False
9.[3p] Which are true of running binary search on a sorted linked list rather than a sorted array?
Which are true of running binary search on a sorted linked list rather than a sorted array?
Select all that apply
The answer is: Reaching the middle element costs hops, destroying the saving, The total cost becomes , The ordering property still holds, so the halving argument is still valid