AQA 7517, section 4.3.4.2
A binary search finds an item in a sorted list by repeatedly halving the part of the list that could contain it. It is much faster than a linear search on large lists, but it only works if the data is sorted.
The idea
Think of looking for a name in a phone book. You don't start on page 1. You open it in the middle, see whether your name comes before or after, and throw away the half that can't contain it. You repeat with the half that is left.
Algorithm
Keep track of the part of the list still to be searched using low and high index values.
- Set
lowto the first index andhighto the last index. - While
low <= high:- find the middle index,
mid = (low + high) // 2; - if the middle item equals the target, it's found;
- if the target is smaller, set
high = mid - 1(ignore the upper half); - if the target is larger, set
low = mid + 1(ignore the lower half).
- find the middle index,
- If
lowbecomes greater thanhigh, the item is not in the list.
def binary_search(items, target):
low = 0
high = len(items) - 1
while low <= high:
mid = (low + high) // 2
if items[mid] == target:
return mid
elif target < items[mid]:
high = mid - 1
else:
low = mid + 1
return -1
data = [3, 8, 12, 19, 25, 31, 40]
print(binary_search(data, 25)) # 4
print(binary_search(data, 10)) # -1
Trace
Searching for 25 in [3, 8, 12, 19, 25, 31, 40] (indexes 0 to 6):
| Step | low | high | mid | Item at mid | Action |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 19 | 25 > 19, so low = 4 |
| 2 | 4 | 6 | 5 | 31 | 25 < 31, so high = 4 |
| 3 | 4 | 4 | 4 | 25 | Found at index 4 |
Only 3 comparisons were needed, instead of up to 7.
An unsuccessful search
Searching for 10 in the same list:
| Step | low | high | mid | Item at mid | Action |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 19 | 10 < 19, so high = 2 |
| 2 | 0 | 2 | 1 | 8 | 10 > 8, so low = 2 |
| 3 | 2 | 2 | 2 | 12 | 10 < 12, so high = 1 |
| 4 | 2 | 1 | low > high, so not found |
Time complexity
Each comparison halves the number of items left. Starting with items, you reach 1 item after about halvings.
| Items in the list | Most comparisons needed |
|---|---|
| 8 | 3 |
| 1 000 | about 10 |
| 1 000 000 | about 20 |
Doubling the size of the list only adds one more comparison. This is logarithmic time, written . A linear search of a million items could need a million comparisons, while a binary search needs around 20.
Requirements and drawbacks
- The list must be sorted first. If it isn't, binary search gives wrong answers.
- It needs to jump straight to the middle item, so it suits a list or array that allows access by index.
- Sorting a list just to search it once may take longer than a linear search. Binary search pays off when you search many times, or when the list is already sorted.
Worked example
Task: how many comparisons does a binary search need, at most, for a sorted list of 100 items?
Keep halving: . That is 7 steps, so at most 7 comparisons. Check against , which rounds up to 7.
Exam tip: you can only use binary search if the data is sorted, so say so. When you trace one, write low, high and mid in a table. Check which way the question rounds the middle when there's an even number of items. Using integer division (rounding down) is usual.
Check yourself
What condition must be true before a binary search can be used?
The list must be sorted.
What is the time complexity of a binary search?
, which is logarithmic time.
Why is a binary search faster than a linear search for large lists?
Each comparison discards half of the remaining items, so the number of comparisons grows very slowly as the list gets larger, whereas a linear search may check every item.
Using the list above, what is the middle item on the first step when searching a sorted list of 9 items (indexes 0 to 8)?
mid = (0 + 8) // 2 = 4, so the item at index 4 is compared first.