13.1 How Binary Search Works
A linear search has to look at every element one by one. For a list of a million items, that's potentially a million comparisons. Binary search finishes the same job in about twenty comparisons. The cost is that the list must already be sorted.
Suppose someone asks: "Is the number 23 in this sorted list?"
List: [ 3, 7, 11, 15, 19, 23, 27, 31, 35 ]
Index: 0 1 2 3 4 5 6 7 8
# Step 1: mid = (0+8)//2 = 4 -> arr[4] = 19. 19 < 23, so discard left half.
# low = 5, high = 8
# Step 2: mid = (5+8)//2 = 6 -> arr[6] = 27. 27 > 23, so discard right half.
# low = 5, high = 5
# Step 3: mid = (5+5)//2 = 5 -> arr[5] = 23. 23 == 23 -> FOUND at index 5!- Binary search compares the middle element to the target.
- If the middle is too small, discard the left half; if too big, discard the right half.
- Repeat on the remaining half until found or the range is empty.
- Set Low = 0, High = 6
- Mid = (0+6)//2 = 3, arr[3] = 11. 11 == 11, found!
- Only takes 1 comparison — the target is at the middle
- First Mid = 3, arr[3] = 11. 11 < 14, go right
- Low = 4, High = 6. Mid = 5, arr[5] = 17. 17 > 14, go left
- Low = 4, High = 4. Mid = 4, arr[4] = 14. Match!
How Binary Search Works
13.1B Interactive Simulator
Try binary search yourself. Change the sorted array and target, then click Start. Use Step for manual control or Play for auto-run. Watch how the range (low/high) narrows and elements get discarded (greyed out) with each comparison.
Simulation
Status: Click Start to begin.
Code
1low = 02high = len(data) - 13while low <= high:4 mid = (low + high) // 25 if data[mid] == target:6 return mid7 elif data[mid] < target:8 low = mid + 19 else:10 high = mid - 111return -113.2 Iterative Binary Search — Pseudocode & Python
Now we turn the description into code. The Cambridge pseudocode below is almost identical to the one in the published mark scheme for 9618/22 M/J 2025 Q7(a).
Cambridge pseudocode
FUNCTION BinarySearch(BYREF dataList : ARRAY OF INTEGER, target : INTEGER) RETURNS INTEGER
DECLARE low, high, mid : INTEGER
low <- 0
high <- LENGTH(dataList) - 1
WHILE low <= high
mid <- (low + high) DIV 2
IF dataList[mid] = target THEN
RETURN mid
ELSE
IF dataList[mid] < target THEN
low <- mid + 1
ELSE
high <- mid - 1
ENDIF
ENDIF
ENDWHILE
RETURN -1
ENDFUNCTIONPython
def binary_search(data_list, target):
low = 0
high = len(data_list) - 1
while low <= high:
mid = (low + high) // 2
if data_list[mid] == target:
return mid
elif data_list[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1 # not found//is Python's integer division (same asDIVin pseudocode) — needed for a valid array index.- Use
mid + 1andmid - 1(not justmid) to avoid infinite loops — mid has already been checked. - The loop condition is
low <= high(not<) — when low == high, one element remains to check.
- Same structure as binary_search but return True/False
- Return True when numbers[mid] == n
- Return False after the loop (not found)
Iterative Binary Search
13.3 Tracing a Binary Search
A trace table tracks low, high, mid, and the comparison result for each iteration. This is what examiners ask you to fill in on Paper 4.
| Iteration | low | high | mid | arr[mid] | Comparison | Action |
|---|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 11 | 11 == 11 | Found! Return 3 |
| — | — | — | — | — | — | Search complete |
- In a trace table question, each row shows one iteration of the while loop.
- Fill in low, high, mid (calculated as (low+high)//2), arr[mid], the comparison result, and the action taken (go left, go right, or found).
- Low=0, High=7. Mid=3, arr[3]=20. 20 < 25, go right
- Low=4, High=7. Mid=5, arr[5]=30. 30 > 25, go left
- Low=4, High=4. Mid=4, arr[4]=25. 25 == 25, found!
- Low=0, High=7. Mid=3, arr[3]=20. 20 > 18, go left
- Low=0, High=2. Mid=1, arr[1]=10. 10 < 18, go right
- Low=2, High=2. Mid=2, arr[2]=15. 15 < 18, go right
- Low=3, High=2. Low > High, loop ends. Return -1.
Tracing & Recursive
13.4 Recursive Binary Search
Recursion replaces the WHILE loop with the function calling itself, each time on a smaller range. Instead of moving Low and High with assignments, we pass new values as parameters.
Cambridge pseudocode (recursive)
FUNCTION RecursiveBinarySearch(
BYREF dataList : ARRAY OF INTEGER,
low : INTEGER, high : INTEGER,
target : INTEGER) RETURNS INTEGER
DECLARE mid : INTEGER
IF low > high THEN
RETURN -1 // base case: not found
ENDIF
mid <- (low + high) DIV 2
IF dataList[mid] = target THEN
RETURN mid // base case: found
ELSE
IF dataList[mid] < target THEN
RETURN RecursiveBinarySearch(
dataList, mid + 1, high, target)
ELSE
RETURN RecursiveBinarySearch(
dataList, low, mid - 1, target)
ENDIF
ENDIF
ENDFUNCTIONPython (recursive)
def recursive_binary_search(
data_list, low, high, target):
if low > high: # base case: empty
return -1
mid = (low + high) // 2
if data_list[mid] == target:
return mid # base case: found
elif data_list[mid] < target:
return recursive_binary_search(
data_list, mid + 1, high, target)
else:
return recursive_binary_search(
data_list, low, mid - 1, target)- Found:
data[mid] == target→ return mid - Not found:
low > high→ return -1 (empty range)
- Without the base case, the function keeps recursing
- The call stack fills up
- Program crashes with stack overflow / RecursionError
13.5 Performance and Conditions
Each iteration of binary search halves the remaining range:
after 1 step: n / 2 elements left
after 2 steps: n / 4 elements left
after 3 steps: n / 8 elements left
after k steps: n / 2^k elements left
The loop ends when n / 2^k <= 1, so k ≈ log2(n).| List size n | Linear search (worst) | Binary search (worst) |
|---|---|---|
| 10 | 10 | 4 |
| 1,000 | 1,000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
| 1,000,000,000 | 1,000,000,000 | 30 |
- 1. The data must be sorted. Without sorting, the halving logic does not work.
- 2. The data structure must support direct access by index. Arrays/lists work; linked lists do not (you cannot jump to the middle).
- log2(32) = 5
- log2(64) = 6
- log2(128) = 7
- Each doubling adds exactly 1 comparison
Performance & Conditions
13.6 Debugging a Buggy Version
Study the pseudocode below. It is supposed to perform a binary search, but it contains four errors. Find each one, give the line number, explain what is wrong, and write the correction.
01 FUNCTION BinarySearch(BYREF B : ARRAY OF INTEGER, target : INTEGER) RETURNS INTEGER
02 DECLARE low, high, mid : INTEGER
03 low <- 0
04 high <- LENGTH(B) // ERROR 1
05 WHILE low < high // ERROR 2
06 mid <- (low + high) DIV 2
07 IF B[mid] = target THEN
08 RETURN mid
09 ELSE
10 IF B[mid] < target THEN
11 low <- mid // ERROR 3
12 ELSE
13 high <- mid // ERROR 4
14 ENDIF
15 ENDIF
16 ENDWHILE
17 RETURN -1
18 ENDFUNCTION- Line 4: high should be LENGTH(B) - 1 (last valid index)
- Line 5: should be WHILE low <= high (not <)
- Line 11: should be low = mid + 1 (not mid, to avoid infinite loop)
- Line 13: should be high = mid - 1 (not mid, to avoid infinite loop)
- / produces a REAL (float), e.g. 3.0
- Array indices must be INTEGER
- B[3.0] would cause a type error / crash
Debugging & Common Errors
13.7 Full Exam-Style Question
A school's library system stores 10,000 book records in a sorted 1D array Catalogue indexed from 0 to 9999. Each element holds a unique 5-digit ISBN (integer). The system uses binary search to find a book by its ISBN.
Catalogue.- The data must be sorted
- The data structure must support direct/random access by index
FindBook(target : INTEGER) RETURNS INTEGER that performs an iterative binary search on the global array Catalogue and returns the index where target is found, or -1 if not present.- low = 0, high = LENGTH(Catalogue) - 1
- WHILE low <= high: mid = (low+high) DIV 2
- Compare Catalogue[mid] to target
- low = mid+1 or high = mid-1
- Return mid if found, -1 after loop
- log2(10000) ≈ 13.29
- So at most 14 comparisons
- Binary search is O(log n)
- Binary: much faster for large sorted lists (O(log n) vs O(n))
- Linear: better for unsorted data or very small lists
Key Points Recap
✓ Key Points Summary
13.8 Practice Tasks
Fifteen exam-style binary search tasks. Each shows only the question — click Hint for the thought process, or Help for the worked solution.
- Correct initialisation (low=0, high=len-1) and loop condition (low <= high).
- Correct mid calculation with integer division: (low+high)//2.
- Correct comparison and range update: mid+1 / mid-1.
- Return mid if found, -1 if not found.
- The most common mark loss is using mid instead of mid+1/mid-1.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
What is the key requirement for binary search?
Question 2True / False
Binary search on 1,000,000 items needs at most ~20 comparisons.
Question 3Multiple Choice
What is the mid formula?
Question 4Multiple Choice
What is the loop condition?
Question 5True / False
When arr[mid] < target, set low = mid + 1 to search the right half.
Question 6Multiple Choice
What is the Big O of binary search?
Question 7Multiple Choice
What are the two conditions for binary search?
Question 8True / False
Using low = mid (instead of mid + 1) can cause an infinite loop.
Question 9Multiple Choice
What does recursive binary search return when not found?
Question 10Multiple Choice
Doubling the list from 32 to 64 adds how many extra comparisons?
Question 11True / False
Binary search is always better than linear search.
Question 12Multiple Choice
What is wrong with high = LENGTH(B) instead of LENGTH(B) - 1?
Answer all 12 questions to enable submission.