A Level · Topic 19.1
A-Level Computer Science 9618 / Topic 19 / A2

Halve it, again.

A binary search finds an item in a sorted list by checking the middle item and throwing away the half that cannot hold it. You write it in pseudocode, trace it with Low, Mid and High, and explain why it needs so few comparisons.

The key idea

A binary search only works on data that is sorted and held in a structure with direct access by index, such as an array. It compares the search item with the middle element, Mid ← (Low + High) DIV 2. If they match, the item is found; if the middle item is smaller, the search continues in the upper half (Low ← Mid + 1); otherwise in the lower half (High ← Mid - 1). It stops when the item is found or Low > High. Each comparison halves the items left, so the worst case for n items is about log2 n comparisons: 1000 items need at most 10.

Binary search for 47: each comparison halves the part still to search 123456789101112131415 3812172126303439424751586370 Mid = (1 + 15) DIV 2 = 8: 34 < 47, so Low ← 9 3812172126303439424751586370 Mid = (9 + 15) DIV 2 = 12: 51 > 47, so High ← 11 3812172126303439424751586370 Mid = (9 + 11) DIV 2 = 10: 42 < 47, so Low ← 11 3812172126303439424751586370 Mid = (11 + 11) DIV 2 = 11: 47 = 47, found after 4 comparisons
Fig. 1: a binary search for 47 in a sorted array of 15 integers. Blue cells are still being searched, grey cells have been discarded.
Section 01

Throw away half.

In the lab, search for a value and step through. Watch the three pointers: Low and High mark the part of the list that could still hold the item, and Mid is the one element compared each time. Then try a value that is not in the list and see Low pass High.

Worked example Trace a search that fails
The array in Fig. 1 holds 3, 8, 12, 17, 21, 26, 30, 34, 39, 42, 47, 51, 58, 63, 70 in elements 1 to 15. Trace a binary search for 20.
  1. Low = 1, High = 15, Mid = 8: List[8] = 34. 34 > 20, so High ← 7.
  2. Low = 1, High = 7, Mid = 4: List[4] = 17. 17 < 20, so Low ← 5.
  3. Low = 5, High = 7, Mid = 6: List[6] = 26. 26 > 20, so High ← 5.
  4. Low = 5, High = 5, Mid = 5: List[5] = 21. 21 > 20, so High ← 4.
  5. Now Low = 5 and High = 4, so Low > High and the loop ends.
Answer:
LowHighMidList[Mid]Action
115834High ← 7
17417Low ← 5
57626High ← 5
55521High ← 4

20 is not found after 4 comparisons (a linear search would need 15).

Your turn
Using the same array, which elements are compared, in order, when searching for 70?
Examiner trap

"A binary search splits the list in half" is not enough for the marks. Say what is compared (the middle item with the search item) and what is discarded (the half that cannot contain it, because the data is sorted).

Section 02

Write it in pseudocode.

Exam questions ask you to write a binary search as a function that returns the index of the item, or −1 if it is not there. Learn this shape; the array name, its bounds and the data type change from question to question.

DECLARE List : ARRAY[1:15] OF INTEGER

FUNCTION BinarySearch(SearchItem : INTEGER) RETURNS INTEGER
    DECLARE Low : INTEGER
    DECLARE High : INTEGER
    DECLARE Mid : INTEGER
    DECLARE Found : BOOLEAN
    Low ← 1
    High ← 15
    Found ← FALSE
    WHILE Low <= High AND Found = FALSE
        Mid ← (Low + High) DIV 2
        IF List[Mid] = SearchItem THEN
            Found ← TRUE
        ELSE
            IF List[Mid] < SearchItem THEN
                Low ← Mid + 1
            ELSE
                High ← Mid - 1
            ENDIF
        ENDIF
    ENDWHILE
    IF Found = TRUE THEN
        RETURN Mid
    ELSE
        RETURN -1
    ENDIF
ENDFUNCTION
Worked example Even-length list
Prices : ARRAY[1:10] holds 5, 11, 14, 19, 23, 28, 36, 40, 44, 52. Using BinarySearch adapted for this array, state the index returned and the number of comparisons for SearchItem = 44.
  1. Low = 1, High = 10: Mid = 11 DIV 2 = 5. Prices[5] = 23 < 44, so Low ← 6.
  2. Low = 6, High = 10: Mid = 16 DIV 2 = 8. Prices[8] = 40 < 44, so Low ← 9.
  3. Low = 9, High = 10: Mid = 19 DIV 2 = 9. Prices[9] = 44: Found ← TRUE.
Answer: The function returns 9 after 3 comparisons
Your turn
For the same array, what is returned when SearchItem = 6, and what are Low and High when the loop ends?
Examiner trap

Do not use the loop condition Low < High. When the item is in the last element left (Low = High) the loop would stop without checking it.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

In the lab, what happens to Low and High after Mid is compared with an item that is larger than List[Mid]?

Quick check+10 XP

Why can a binary search not be used directly on a linked list?

Quick check+10 XP

Low = 6 and High = 11. What is Mid?

Quick check+10 XP

When does a binary search stop if the item is not in the array?

Section 03

How fast is fast?

The syllabus asks how the performance of a binary search varies with the number of items. The answer is that doubling the data adds only one comparison, because one comparison halves it straight back.

Number of items nLinear search, worst caseBinary search, worst case
15154
1001007
1000100010
1024102411
1 000 0001 000 00020
Worked example Doubling the data
A sorted array of 500 customer IDs is searched with a binary search. The array grows to 4000 IDs. By how much does the worst-case number of comparisons rise?
  1. 500: 28 = 256 ≤ 500 < 512 = 29, so INT(log2 500) = 8 and the worst case is 9.
  2. 4000: 211 = 2048 ≤ 4000 < 4096 = 212, so INT(log2 4000) = 11 and the worst case is 12.
  3. The data grew 8 times (three doublings), and the comparisons rose by 3.
Answer: From 9 to 12 comparisons: only 3 more for 8 times the data. A linear search would rise from 500 to 4000.
Your turn
What is the largest number of items a binary search can always handle in at most 6 comparisons?
Examiner trap

Do not say a binary search is "always faster". It needs sorted data, and on a tiny list, or when the item is the first element, a linear search can need fewer comparisons.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is the maximum number of comparisons a binary search makes on a sorted array of 200 items?

Exam style+20 XP

Which statement about a binary search is correct?

Exam style+20 XP

A programmer writes Low ← Mid instead of Low ← Mid + 1. What is the most likely problem?

Exam style+20 XP

An array of sorted data grows from 1000 to 2000 items. How does the worst case of a binary search change?

Skill unlocked

Binary search, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Writing and tracing a binary search in pseudocode, its conditions, and how its performance varies with the number of items.