- Explain and apply linear search
- Carry out binary search on a sorted list step by step
- Estimate how many comparisons each method needs
Murad thinks of a number from 1 to 100, and Aysel has to find it. After each guess, Murad only says “higher”, “lower” or “correct”. If Aysel asks 1, 2, 3… in order, she might need 100 guesses. But if she starts at 50 and halves the range each time, she will find the number in at most 7 guesses. These two approaches are two famous algorithms: linear search and binary search.
Linear search
In linear (sequential) search, the items of a list are compared with the target one by one, starting from the first. When a match is found, the search stops; if we reach the end of the list without a match, the item is not there.
The advantage of linear search is that it is simple and works on any list, even an unsorted one. The drawback is speed: in the worst case, a list of n items needs n comparisons.
Use linear search to find 23 in the list [7, 3, 23, 9, 15].
Show solutionHide solution
Comparison 2: 3 ≠ 23 → keep going.
Comparison 3: 23 = 23 → found!
The item is in 3rd place (in programming, numbering starts at 0, so its index is 2). It took 3 comparisons.
def linear_search(items, target):
for i in range(len(items)):
if items[i] == target:
return i
return -1
numbers = [7, 3, 23, 9, 15]
print(linear_search(numbers, 23))
print(linear_search(numbers, 4))▸ Expected output
2 -1
Binary search
Binary search works only on a sorted list (for example, in increasing order), but it is very fast. At each step the target is compared with the middle item of the range, and half of the list is thrown away at once.
- 1Find the middle
Take the item in the middle of the current search range.
- 2Compare
If the middle item equals the target, it is found and the search is over.
- 3Drop half
If the target is smaller than the middle item, keep searching in the left half; if it is bigger, in the right half.
- 4Repeat
Repeat steps 1–3 until the item is found or the range is empty. An empty range means the item is not in the list.
Use binary search to find 37 in the sorted list [3, 8, 12, 17, 23, 31, 37, 42, 50]. The indexes of the items are 0 to 8.
Show solutionHide solution
Step 2: middle index (5 + 8) ÷ 2 = 6 (whole part), item 37. 37 = 37 → found!
It took only 2 comparisons. Linear search would have needed 7.
def binary_search(items, target):
low, high = 0, len(items) - 1
steps = 0
while low <= high:
mid = (low + high) // 2
steps += 1
if items[mid] == target:
return mid, steps
elif items[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1, steps
numbers = [3, 8, 12, 17, 23, 31, 37, 42, 50]
print(binary_search(numbers, 37))
print(binary_search(numbers, 5))▸ Expected output
(6, 2) (-1, 3)
Which one is faster?
In binary search every comparison halves the list, so doubling the list adds just one more comparison. For large lists the difference is amazing:
| Number of items | Linear search | Binary search |
|---|---|---|
| 10 | 10 | 4 |
| 100 | 100 | 7 |
| 1000 | 1000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
Key points
- Linear search checks items one by one and works on any list.
- On a list of n items, linear search needs up to n comparisons.
- Binary search works only on a sorted list and halves the range at every step.
- Binary search needs at most 10 comparisons for 1000 items and 20 for a million.
Check yourself
10 questions. Every correct answer earns XP.