RecallStrideA BreakellSystems productTry revision

Linear search vs binary search: examples and trace tables

Trace both searches on the same list and explain why binary search needs sorted data.

A search algorithm tries to find a target in a collection. Linear search checks entries in order. Binary search uses the ordering of a sorted collection to discard roughly half the remaining entries after each unsuccessful comparison. Choosing between them requires more than remembering which is usually faster.

From the short lesson: trace a missing value

Sort [14, 3, 25, 8, 19] into [3, 8, 14, 19, 25]. To find 12, start with low = 0 and high = 4. The middle is index 2: value 14 is too large, so high becomes 1. The next middle is index 0: value 3 is too small, so low becomes 1. Index 1 contains 8, also too small, so low becomes 2. Now low > high: stop and report not found. The compared values were 14 → 3 → 8. Never invent a match when the interval is empty.

Your turn: search the same sorted list for 19. The first comparison is 14; move low to 3. The next middle is index 3, containing 19: found. A successful search need not inspect every item.

Try the free GCSE binary-search task →

Try the free A-Level linear-search task →

Linear search checks one entry at a time

Consider the list [3, 7, 11, 18, 24, 31, 42]. To find 24, a linear search compares the target with 3, 7, 11, 18 and then 24. It succeeds after five item comparisons. If the target were 3 it would succeed immediately. If the target were 50, a basic linear search would inspect all seven entries and report that it was absent.

The list does not have to be sorted. Linear search is useful for small collections, unsorted data, and structures that are naturally accessed sequentially. Its worst-case number of item comparisons grows in proportion to the list length: O(n).

Binary search narrows a sorted interval

Use zero-based indices and inclusive low/high bounds. Set low to 0 and high to 6. Calculate the middle index as (low + high) DIV 2. If the middle value is smaller than the target, move low to middle + 1. If it is larger, move high to middle − 1. A match ends the search.

Binary search for 24 in [3, 7, 11, 18, 24, 31, 42]
StepLowHighMiddleValueNext action
10631824 is larger: low = 4
24653124 is smaller: high = 4
344424Found at index 4

This counts one target-to-item comparison at each middle position. Counting every Boolean operation in a particular implementation gives a different total; state what you are counting.

Why must the data be sorted?

When the middle value is 18, rejecting the left half is justified only because all those values are no larger than 18. An unsorted list does not supply that guarantee. Binary search can incorrectly discard the section containing the target. Sorting first also costs work, so sorting a list only to perform one search is not automatically the best choice.

Compare the algorithms

Basic array-search comparison
PropertyLinearBinary
Sorted data requiredNoYes
Best caseFirst checked entry matches: O(1)First midpoint matches: O(1)
Worst caseO(n)O(log n) item checks with efficient indexed access
ProgressAdvance by one entryDiscard about half the remaining interval

Test the boundary cases

Check an empty list, one entry, a target at each end, an absent target below the smallest value, an absent target above the largest, and an absent target between two entries. With inclusive bounds, continue while low ≤ high; stop unsuccessfully when low > high. Updating a bound to middle without adding or subtracting one can prevent progress.

Practice: how does binary search reject 25 in the example list?

Check 18 at index 3, then 31 at index 5, then 24 at index 4. Move low to 5. High is still 4, so the remaining interval is empty and 25 is absent.

Practice: which search can be used directly on [18, 3, 42, 7]?

Linear search. Binary search requires the list to be ordered before its discard-half reasoning is valid.

For GCSE, practise describing and tracing the methods accurately. At A-Level, also connect the shrinking search interval to logarithmic complexity and consider the cost of preparing the data.

Related course topics

OCR J277 2.1.3 · OCR H446 2.3.1

Scope and sources

These original RecallStride explanations and examples are for study. Consult the OCR GCSE J277 specification (PDF) and OCR A-Level H446 specification (PDF) for qualification requirements. Not every detail applies at the same depth to both courses. Read the editorial policy or report an error to support@recallstride.com.

Continue learning