True or false: Linear search can be used to find a target in an unsorted list.
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.
A program must locate one value in the small unsorted list [14, 3, 27, 8, 19]. Which algorithm is the most appropriate choice?
- A
Binary search, because it always uses fewer comparisons
- B
Linear search, because the collection is unsorted and only one small search is needed
- C
Binary search, because it works on every list
- D
Either algorithm, because sorted order never matters
What is the name of the algorithm that repeatedly compares a target with the middle element of a sorted collection and halves the remaining interval?
Which condition is essential for ordinary binary search to be correct?
- A
The list must contain unique values
- B
The list must be sorted according to the comparison rule
- C
The target must be the first or last element
- D
The list must be stored as a linked list
Which conditions generally make binary search an appropriate choice? Select all correct answers.
- A
The collection is already sorted and remains sorted during searching
- B
The collection supports efficient access to a middle element
- C
The collection is unsorted and will be searched only once
- D
Many searches will be performed on the collection
True or false: Applying ordinary binary search to an unsorted list can discard the half containing the target and return an incorrect result.
- A
True
- B
False
Using ordinary binary search on the ascending list [4, 9, 15, 22, 31], how many element comparisons are made to find the target 22? Enter a whole number; no tolerance is allowed.
Complete the invariant: If the target exists, it must remain within the current interval from .
Which expression is preferred in some languages for computing a binary-search middle index because it helps avoid integer overflow?
- A
(low + high) // 2
- B
low + high // 2
- C
low + (high - low) // 2
- D
(high - low) // 2
A sorted list may contain duplicate target values. Which actions are appropriate when modifying binary search to find a boundary occurrence? Select all correct answers.
- A
For the first occurrence, record a match and continue searching the lower half
- B
For the first occurrence, stop immediately at any match
- C
For the last occurrence, discard every value after a match
- D
For the last occurrence, record a match and continue searching the upper half
Explain when linear search is preferable to binary search and when binary search is preferable to linear search. Include the relevant data-ordering and access assumptions, worst-case time complexities, and the effect of sorting cost.
Why may binary search provide less practical benefit on a linked list than on an array with fast indexed access, even though its comparison count is logarithmic?
- A
Binary search is impossible on any linked list
- B
Binary search may lose much of its practical advantage because reaching middle positions can require traversal
- C
Linear search becomes O(log n) on a linked list
- D
A linked list is automatically sorted
A program must search a small collection whose elements are not sorted, and the collection will be searched only occasionally. Which approach is most appropriate?
- A
Use linear search.
- B
Use binary search without sorting.
- C
Use binary search only if the target is larger than the first element.
- D
Use a boundary search that requires sorted data.
What condition is essential for binary search to safely discard half of its current search interval?
- A
The collection must contain unique values.
- B
The collection must be sorted according to the comparison rule.
- C
The collection must have an even number of elements.
- D
The target must occur at an endpoint.
Binary search examines the sorted list [3, 8, 12, 17, 24, 31, 42, 56] and compares target 31 with the middle value 17. Which interval should it retain for the next search?
- A
Search indices 0 through 2.
- B
Search indices 1 through 4.
- C
Search indices 4 through 7.
- D
Stop and report that 31 is absent.
A small unsorted collection will be searched exactly once. Why might linear search be preferable to sorting the collection and then using binary search?
- A
Binary search is always faster whenever it is available.
- B
Binary search never works on collections with fewer than 10 elements.
- C
Linear search requires the collection to be sorted first.
- D
Linear search may be preferable because sorting has a cost and only one small search is needed.
True or false: When duplicate values are present, ordinary binary search may return any one of the matching occurrences rather than necessarily the first or last occurrence.
- A
True
- B
False
What Python module provides bisection operations for locating positions in an already sorted sequence?
A zero-based linear search examines the list [14, 6, 29, 11] for target 11. What index does it return?
For a linear search through a list of n elements, if the target is absent, the algorithm performs comparisons before reporting that the target is not found.