AQA GCSE Computer Science (8525), section 3.1.3 (Searching algorithms)
How it works
A binary search only works on a list that is already sorted. It repeatedly looks at the middle item and throws away the half that cannot contain the target. Each check halves the amount of data left to search.
Steps
- Find the middle item of the part of the list still being searched.
- If it is the target, stop: found.
- If the target is smaller than the middle item, discard the middle and everything after it: search the left half.
- If the target is larger, discard the middle and everything before it: search the right half.
- Repeat. If there is nothing left to search, the item is not in the list.
Worked example
Find 40 in this sorted list (positions 0 to 8):
| Position | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Value | 3 | 8 | 12 | 19 | 25 | 31 | 40 | 47 | 56 |
| Step | Search from | to | Middle | Middle value | Result |
|---|---|---|---|---|---|
| 1 | 0 | 8 | 4 | 25 | 40 is bigger, so search the right half |
| 2 | 5 | 8 | 6 | 40 | Found |
Only 2 checks were needed. A linear search would have taken 7.
Searching for something that is not there: 10
| Step | Search from | to | Middle | Middle value | Result |
|---|---|---|---|---|---|
| 1 | 0 | 8 | 4 | 25 | 10 is smaller, search the left |
| 2 | 0 | 3 | 1 | 8 | 10 is bigger, search the right |
| 3 | 2 | 3 | 2 | 12 | 10 is smaller, search the left |
| 4 | 2 | 1 | nothing left, not found |
Python
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, 47, 56]
print(binary_search(data, 40)) # 6
print(binary_search(data, 10)) # -1
If the part being searched has an even number of items, there are two middle items; either can be used as long as you are consistent.
Linear search or binary search?
| Linear search | Binary search | |
|---|---|---|
| List must be sorted? | No | Yes |
| Speed on a long list | Slow | Much faster |
| Difficulty | Simple | Slightly harder to write |
| Best used for | short or unsorted lists | long sorted lists |
The number of checks for binary search grows very slowly: a sorted list of about 1000 items needs at most 10 checks, and one of about 1 000 000 items needs at most 20.
Common mistake: using a binary search on a list that is not sorted: it will give wrong answers. Also, after comparing with the middle item, you must discard that half, not just look at the item next to it.
Exam tip: in a binary search question, show the middle item at each step and say which half is discarded. Remember to mention that the list must be sorted when comparing the two searches.
Check yourself
What must be true of a list before a binary search can be used?
It must be sorted.
In the list 2, 4, 6, 8, 10, 12, 14, which is the middle item?
Give one advantage and one disadvantage of binary search compared with linear search.
It is much faster on long lists, but the list must be sorted first.