ITPEC FE Morning October 2019 Question 8
The binary search algorithm is used to search for a given item when items are sorted. If the number of items is 1 million, which of the following is the closest to the maximum number of comparisons required to find the item?
Explanation
20 — each comparison halves the items still in range, so the maximum is the number of halvings that bring 1 million down to one item.
- •219 = 524,288, fewer than 1 million items
- •220 = 1,048,576, at least 1 million items
- •The base-2 logarithm of 1,000,000 is about 19.9, so at most 20 comparisons are needed
Answer (b)
Why not others:
- •(a) 15 — 215 = 32,768: 15 comparisons cover only about 33 thousand items
- •(c) 25 — 225 is about 33.5 million: enough for 33 times as many items
- •(d) 30 — 230 is about 1.07 billion: the count for a thousand times as many items
Key rule: Binary search needs at most about log₂ n comparisons; since 210 is about 1,000, every factor of 1,000 in n adds about 10 comparisons.
Algorithm and Programming · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.