Q.Estimate the number of key comparisons required in binary search and linear search if we need to find the details of a person in a sorted database having 2^30 (1,073,741,824) records when details of the person being searched lies at the middle position in the database. What do you interpret from your findings?
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The person's record lies at the middle of a sorted database of n = 2³⁰ = 1,073,741,824 records. Binary search starts at the middle, so it finds the record in 1 comparison. Linear search starts at record 1 and must walk all the way to the middle: 2²⁹ = 536,870,912 comparisons. The interpretation is that a sorted database plus binary search converts an O(n) problem into an O(log n) one.
Concept understanding
The two techniques differ in the information they exploit.
- Linear search assumes nothing about the order of the records. Its only strategy is to compare the key with record 1, record 2, record 3, … until it matches. Its cost is proportional to the position of the target.
- Binary search exploits the fact that the records are sorted. It compares the key with the middle record; that single comparison tells it which half the key must lie in, and the other half — half a billion records — is discarded untouched.
The question chooses the target position deliberately: the middle. That is simultaneously binary search's best case and roughly linear search's average case, which makes the contrast as sharp as it can be.
Binary search — the count
| Iteration | first | last | mid | Result |
|---|---|---|---|---|
| 1 | 0 | 2³⁰ − 1 = 1,073,741,823 | (0 + 1,073,741,823) // 2 = 536,870,911 | that is the middle record → key found |
mid = (first + last) // 2
With first = 0 and last = n − 1, the first mid is the middle position of the whole database.
Binary search needs 1 key comparison.
Linear search — the count
Linear search compares the key against record 1, then record 2, and so on, stopping only when it reaches the target. The target is the middle record, i.e. the (n/2)-th record.
Comparisons = position of the key = n / 2 = 2³⁰ / 2 = 2²⁹ = 536,870,912
Linear search needs about 536.87 million key comparisons.
Side-by-side
| Linear search | Binary search | |
|---|---|---|
| Precondition | none | list must be sorted |
| Comparisons for this key (middle) | 536,870,912 | 1 |
| Best case | 1 (key is first) | 1 (key is at the middle) |
| Worst case | n = 1,073,741,824 | ⌈log₂ n⌉ = 30 |
| Time complexity | O(n) | O(log₂ n) |
Why binary search's worst case is only 30
Each comparison halves the number of surviving records:
| Comparisons made | Records still to be searched |
|---|---|
| 0 | 2³⁰ = 1,073,741,824 |
| 1 | 2²⁹ = 536,870,912 |
| 2 | 2²⁸ = 268,435,456 |
| … | … |
| 29 | 2¹ = 2 |
| 30 | 2⁰ = 1 |
After 30 halvings only one record remains, so 30 comparisons settle any search in a billion-record sorted database.
Verifying with a program
import math
n = 2 ** 30 # 1,073,741,824 records
linear_comparisons = n // 2 # key lies at the middle position
binary_comparisons = 1 # binary search probes the middle first
binary_worst_case = math.ceil(math.log2(n))
print('Records in database :', n)
print('Linear search comparisons :', linear_comparisons)
print('Binary search comparisons :', binary_comparisons)
print('Binary search worst case :', binary_worst_case)
print('Speed-up factor :', linear_comparisons // binary_comparisons)
Output
Records in database : 1073741824
Linear search comparisons : 536870912
Binary search comparisons : 1
Binary search worst case : 30
Speed-up factor : 536870912
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.