In the iterative binary-search implementation that uses the half-open interval [low, high), which initial value of high correctly represents the entire array A?
08 Searching Algorithms Online Quiz Questions
Use this free practice quiz with 20 questions to review 08 Searching Algorithms, test your knowledge, and prepare for your next test or exam.
What does lower-bound search return for a sorted array?
- A
The last index whose value is strictly less than the target
- B
Any index containing a value equal to the target
- C
The first index i such that A[i] is greater than or equal to the target
- D
The index at which the target would be inserted after all equal values
Why does binary search on a sorted linked list usually lose the usual time advantage it has on an array?
- A
It may require linear-time traversal to locate middle positions
- B
It always performs constant-time lookup of every middle position
- C
It cannot compare elements because linked lists have no ordering
- D
It necessarily uses logarithmic auxiliary space
Which conditions are required for binary search to be correct and efficient during a search? Select all that apply.
- A
The data is sorted according to the comparison ordering
- B
The target and elements have compatible comparison operations
- C
The collection must never be modified at any time in the future
- D
The implementation can access middle positions efficiently
- E
The collection remains ordered during the search
Which situations favor sequential search over binary search? Select all that apply.
- A
The collection is unsorted
- B
The same large sorted collection will receive many searches
- C
Values are accessed through a stream or linked structure
- D
The collection changes so often that maintaining sorted order is expensive
True or false: On a random-access array, the recursive version of binary search uses logarithmic auxiliary stack space because its recursion depth is logarithmic.
- A
True
- B
False
True or false: The iterative sequential-search implementation uses constant extra space regardless of the input size.
- A
True
- B
False
Using zero-based indexing, what index does sequential search return when searching for 23 in [8, 14, 23, 41]?
What is the other standard name for sequential search?
Complete the loop invariant for sequential search: Before each iteration, every position before the current index has been and does not contain the target.
Complete the definition: For a sorted array, lower-bound search returns the .
Explain why binary search is especially valuable for many searches on a stable sorted array, but may not be the best choice for one search or a frequently changing collection.
Which midpoint expression is preferred in fixed-width integer languages because it avoids overflow when low and high are large?
- A
(low + high) // 2
- B
low + high // 2
- C
low + (high - low) // 2
- D
(high - low) // 2
A program must search a small collection whose elements are frequently inserted and are not kept in sorted order. Which search algorithm is the most appropriate choice?
- A
Binary search
- B
Sequential search
- C
Lower-bound search
- D
Recursive binary search
Why must a collection be sorted before the standard binary-search algorithm is applied?
- A
The target must be stored at index 0 or at the last index.
- B
The collection must contain no duplicate values.
- C
The data must be sorted according to the comparison ordering.
- D
The collection must be implemented as a linked list.
What is the name of the binary-search variant that returns the first index i such that A[i] is greater than or equal to the target?
In the half-open binary-search implementation, what update is correct when A[mid] is less than the target?
- A
low = mid + 1
- B
low = mid
- C
high = mid + 1
- D
high = mid - 1
Which search algorithm is generally the more suitable choice for an unsorted linked list when the elements must be examined through sequential links?
An already sorted array of n elements is searched with iterative binary search, and the target is absent. What is the worst-case running time?
- A
Θ(1) in the worst case because the middle element is always the answer
- B
Θ(n) in the worst case because every element may be compared
- C
Θ(n log n) because the array must be sorted during every search
- D
Θ(log n) in the worst case when the array is already sorted
True or false: When ordinary binary search is applied to the sorted array [2, 4, 4, 4, 9] with target 4, it is guaranteed to return the index of the first 4.
- A
True
- B
False