Skip to main content
EnglandComputer ScienceSyllabus dot point

How do computers find an item in a list quickly?

Understand and explain how the linear search and binary search algorithms work, trace each one, and compare them including the requirement that binary search needs a sorted list.

A focused answer to AQA GCSE Computer Science 3.1.3, covering how linear search and binary search work, how to trace each one, and how they compare including why binary search needs a sorted list.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Linear search
  3. Binary search
  4. Comparing the two
  5. How the two algorithms work step by step
  6. Efficiency in numbers
  7. Try this

What this dot point is asking

AQA wants you to explain how linear search and binary search work, trace them on a list of values, and compare them, including knowing that binary search only works on a sorted list.

It works on any list, sorted or not, which is its main advantage. The downside is that for a list of nn items it may need up to nn comparisons (when the target is last or absent), so it is slow for large lists. On average it checks about half the list.

Because each step roughly halves the number of items left to check, binary search is far quicker than linear search on large lists. The reason it needs a sorted list is that the "discard a half" rule is only valid when the values are in order, so the algorithm knows which half cannot contain the target.

Comparing the two

The trade-off is that if a list is unsorted and you only search it once, the cost of sorting it first may outweigh the saving, so linear search can be the sensible choice. Binary search pays off on large lists that are already sorted or searched many times.

How the two algorithms work step by step

A linear search keeps a position that starts at the first element and moves one place right after each comparison, checking each item against the target and stopping if it matches or when it runs off the end. A binary search keeps two pointers, the lowest and highest positions still in play, and repeatedly examines the middle item between them: if the middle matches, it stops; if the target is smaller, it moves the high pointer just below the middle; if larger, it moves the low pointer just above the middle, halving the range each time. Describing the algorithms in terms of these moving positions is exactly what a "describe how the algorithm works" question rewards.

Efficiency in numbers

The efficiency difference is dramatic and worth quoting with figures. For a list of nn items, linear search makes up to nn comparisons, so doubling the list doubles the worst-case work. Binary search makes at most about log2n\log_2 n comparisons, so doubling the list adds only one extra comparison. For 1000 items that is up to 1000 versus about 10; for a million items it is up to a million versus about 20. This is why binary search is preferred for large, already-sorted data, while linear search remains useful for short or unsorted lists where sorting first would not pay off.

Try this

Q1. State one condition that must be true for a binary search to work. [1 mark]

  • Cue. The list must be sorted (in order).

Q2. A list has 1024 sorted items. State the maximum number of items a binary search must check. [1 mark]

  • Cue. About 10, because log21024=10\log_2 1024 = 10 and each step halves the list.

Exam-style practice questions

Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AQA 20204 marksThe sorted list [2, 5, 8, 12, 16, 23, 38, 45] is searched for the value 23 using a binary search. Trace the search, stating the middle value examined at each step and the part of the list kept, and give the number of comparisons made.
Show worked answer →

Step 1: with 8 items, take the middle (position 4, value 12). 23>1223 > 12, so discard the lower half and keep [16, 23, 38, 45].

Step 2: middle of the remaining four (value 23 or 38 depending on rounding; AQA accepts either midpoint). Taking 38: 23<3823 < 38, so keep [16, 23]. Taking 23 first instead would find it in this step.

Step 3: middle of [16, 23] is 23, which matches. The target is found in 3 comparisons (2 if the earlier midpoint lands on 23).

Markers reward a consistent midpoint rule, correctly discarding the half that cannot contain 23, and a comparison count consistent with the trace.

AQA 20234 marksCompare linear search and binary search. State one advantage of each, and explain why binary search cannot be used on the list [7, 2, 9, 4].
Show worked answer →

Advantage of linear search: it works on any list, sorted or not, and needs no sorting first, so it is simple and fine for short or unsorted data. Advantage of binary search: on a large sorted list it is far faster, needing at most about log2n\log_2 n comparisons instead of up to nn.

Binary search cannot be used on [7, 2, 9, 4] because the list is not sorted. Binary search relies on discarding the half that cannot contain the target, which is only valid if the values are in order; on an unsorted list it could discard the half that actually holds the target and miss it.

Markers reward one valid advantage of each and the explicit reason (the list is unsorted, so halving is invalid).

Related dot points

Sources & how we know this