Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Searching a sorted array

1.[2p]

How many comparisons does binary search need in the worst case on an array of 109 elements?

CorrectNot quite: 30

2.[2p]

What invariant does binary search maintain?

Correct
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?

CorrectNot quite: 3

4.[3p]

The decision-tree lower bound for comparison-based search on n elements is log2(n+1) because

Correct
The answer is: the algorithm needs at least $n+1$ distinct leaves, and a binary tree of height $h$ has at most $2^h$
The answer is: the algorithm needs at least $n+1$ distinct leaves, and a binary tree of height $h$ has at most $2^h$
The answer is: the algorithm needs at least $n+1$ distinct leaves, and a binary tree of height $h$ has at most $2^h$

5.[2p]

Interpolation search can beat log2n because

Correct
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 mid=lo+(hi-lo)/2 preferred to (lo+hi)/2?

Correct
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?

CorrectNot quite: 3

8.[2p]

Binary search on an array that is not sorted reports an error rather than a wrong answer.

The answer is: False
Correct

9.[3p]

Which are true of running binary search on a sorted linked list rather than a sorted array?

Select all that apply

Correct
Correct
Correct
The answer is: Reaching the middle element costs $\Theta(n)$ hops, destroying the saving, The total cost becomes $\Theta(n)$, The ordering property still holds, so the halving argument is still valid