By LearnAllStudents Editorial Team. Source check dates are listed below.
Worked trace: find 27
Scroll the table to see all columns. Keyboard: focus the table area and use the arrow keys.
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Value | 3 | 8 | 12 | 19 | 27 | 34 | 41 |
Use inclusive bounds low=0 and high=6 and round the middle index down. First mid=3, value 19. Since 27 is greater, set low=4. The remaining range is indices 4 to 6. Next mid=5, value 34; set high=4. Now mid=4, value 27, so return index 4. This trace uses three middle-item checks. It does not return ‘the fourth value’: the answer is an index using the stated zero-based convention.
Use consistent pseudocode
low ← 0
high ← length(items) - 1
WHILE low <= high
mid ← low + FLOOR((high - low) / 2)
IF items[mid] = target THEN RETURN mid
IF items[mid] < target THEN
low ← mid + 1
ELSE
high ← mid - 1
RETURN NOT_FOUNDThe +1 and −1 exclude the already-tested middle item. Without them, a one-item interval can repeat forever. The loop includes equality because a single remaining item still needs testing. This version returns any found occurrence if duplicates exist; finding the first or last occurrence needs additional boundary logic.
Why the growth is logarithmic
After one unsuccessful check, roughly half the candidate positions remain; after two, roughly a quarter; after three, roughly an eighth. A list of 1,023 items can be resolved in at most 10 middle-item checks with this model. Doubling the size adds about one check, rather than doubling the work. Big-O describes how cost grows, not the exact elapsed time on a particular computer.
The model assumes direct access to a middle position and a constant-cost comparison. Traversing a linked list to reach each middle changes the cost. Sorting an unsorted dataset also has a cost, so ‘binary search is always better’ is not a sufficient system-level argument. A one-off search in a tiny unsorted list may be simpler to perform linearly.
Try it yourself
- Using the seven-item list above, search for 10 and write each middle index.
- What should an empty-list search return in the stated pseudocode?
- Why is low=mid potentially unsafe?
- If 27 occurs twice, does the basic algorithm promise the first occurrence?
Answers: middle indices 3, 1 and 2 are tested, then low=2 and high=1 so the search ends without a match; an empty list returns NOT_FOUND without indexing an item; low=mid can leave the range unchanged; no, the basic algorithm can return either occurrence. For the failed search, keep a table of low, high and mid rather than trying to visualise all changes at once.
For parents: ask what was safely discarded
Give your child a sorted list and a target, then ask them to explain why each removed half cannot contain the target. Change the list to an unsorted order and ask whether the argument still holds. This tests the precondition instead of arithmetic alone. When reviewing code, test an empty list, a one-item match, a one-item miss and a target beyond the final value. Each case reveals a different boundary mistake.
How a tutor would approach this
Bring the search trace or pseudocode. Focus on range boundaries and the stated comparison model before trying to memorise complexity notation.
Frequently asked questions
Must the list be sorted?
Yes, under the same ordering used by the search comparisons. Otherwise discarding half is unjustified.
Is O(log n) an exact time?
No. It describes growth in the chosen cost model, not a number of seconds.
Why move beyond mid?
The middle item has already been checked. Excluding it makes progress and prevents a repeated interval.
Does it always find the first duplicate?
The basic version does not. A first-occurrence search needs additional logic.
Official sources
- AQA GCSE Computer Science: algorithms (checked 2026-11-27)
- AQA A Level Computer Science: algorithms (checked 2026-11-27)
Related pages
Try a free 30-minute demo
Work through a question with a tutor. No payment needed.
Request a free 30-minute demo