Let πΉ(π) denote the maximum number of comparisons made while searching forβ¦
2024
Let πΉ(π) denote the maximum number of comparisons made while searching for an entry in a sorted array of size π using binary search.
Which ONE of the following options is TRUE?
- A.
πΉ(π) = πΉ(βπ/2β) + 1
- B.
πΉ(π) = πΉ(βπ/2β) + πΉ(βπ/2β)
- C.
πΉ(π) = πΉ(βπ/2β)
- D.
πΉ(π) = πΉ(π β 1) + 1
Attempted by 146 students.
Show answer & explanation
Correct answer: A
Answer: The correct recurrence for the worst-case number of comparisons in binary search is F(n) = F(floor(n/2)) + 1.
Base cases: F(0) = 0 (no elements to check) and F(1) = 1 (one comparison).
Recurrence derivation: At each step binary search compares the middle element (counts as 1) and then continues searching in at most floor(n/2) remaining elements. Hence for n β₯ 2, F(n) = F(floor(n/2)) + 1.
Closed form: Repeatedly halving n until it becomes 1 takes floor(log2 n) steps, and each step contributes one comparison. Including the final comparison gives F(n) = floor(log2 n) + 1.
Therefore the provided recurrence is correct, and the worst-case number of comparisons grows as Ξ(log n).