Skip to main content
EnglandComputer ScienceSyllabus dot point

How do linear and binary search work, and which is faster?

Understand the linear search and binary search algorithms, how each works, their requirements, and their time complexity.

A focused answer to AQA A-Level Computer Science 4.3.2, covering the linear search and binary search algorithms, how each works, the requirement that binary search needs sorted data, and their time complexity.

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 them

What this dot point is asking

AQA wants you to describe how linear search and binary search work, state that binary search requires a sorted list, give the time complexity of each, and trace either algorithm on supplied data.

FOR i = 0 TO length - 1
  IF list[i] = target THEN RETURN i
ENDFOR
RETURN -1   # not found

Linear search has time complexity O(n)O(n): in the worst case (item last or absent) it makes nn comparisons. Its best case is O(1)O(1) when the target is the first item, and the average case is roughly n/2n/2 comparisons, which is still O(n)O(n). Its strength is that it needs no preparation of the data.

WHILE low <= high
  mid = (low + high) DIV 2
  IF list[mid] = target THEN RETURN mid
  ELSE IF list[mid] < target THEN low = mid + 1
  ELSE high = mid - 1
ENDWHILE
RETURN -1   # not found

The bounds low and high define the part of the list still being searched; updating them to mid + 1 or mid - 1 (never mid itself) guarantees the search space shrinks every loop, so the algorithm always terminates.

Comparing them

Binary search is dramatically faster for large lists: searching a million items takes up to about 20 comparisons rather than a million, because log2100000020\log_2 1\,000\,000 \approx 20. The trade-off is that the list must be sorted first, which has its own cost (an O(nlogn)O(n \log n) sort); for a small list, an unsorted list, or a list searched only once, linear search may be the simpler and faster overall choice.

A useful way to decide is to think about how often you search versus how often the data changes. If a list is searched many thousands of times and rarely changes, paying the one-off cost of sorting it so that every subsequent search is O(logn)O(\log n) is clearly worthwhile. If the data is constantly changing or each search happens only once, the sort cost is never recovered, so a plain linear scan wins. This is why a database keeps sorted indexes (built once, searched constantly) while a one-off scan through a log file uses linear search.

Both algorithms can be written either iteratively (using a loop) or recursively (the function calls itself on the smaller sub-problem). Binary search lends itself naturally to recursion because each step solves a smaller version of the same problem on half the list, and the recursion depth of about log2n\log_2 n is exactly why its time complexity is logarithmic. Examiners frequently ask you to trace either version step by step, so practise writing out the index examined and the bounds at every iteration rather than just stating the final answer.

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 20184 marksA binary search is performed on the sorted list [3, 8, 12, 19, 25, 31, 47, 52] to find the value 31. Trace the algorithm, stating the index examined at each step and the value found there, until the target is located. Indices start at 0.
Show worked answer →

Set low =0= 0, high =7= 7.

Step 1: mid =(0+7)DIV2=3= (0 + 7)\,\text{DIV}\,2 = 3, list[3] =19= 19. 19<3119 < 31, so search the upper half: low =4= 4.

Step 2: mid =(4+7)DIV2=5= (4 + 7)\,\text{DIV}\,2 = 5, list[5] =31= 31. Match found at index 5.

Markers reward correctly computing each midpoint using integer division, comparing with the target, narrowing the bounds the right way, and reaching index 5 in two steps. Showing the bound updates is what earns the method marks.

AQA 20213 marksExplain why binary search has a time complexity of O(log n) while linear search has a time complexity of O(n), and state the requirement binary search places on the data.
Show worked answer →

Linear search checks items one at a time from the start, so in the worst case (target last or absent) it makes nn comparisons, giving O(n)O(n): the work grows in direct proportion to the list size.

Binary search halves the remaining search space at each comparison, so after kk comparisons at most n/2kn / 2^k items remain. The number of steps to reduce nn to 1 is about log2n\log_2 n, hence O(logn)O(\log n). This requires the data to be sorted, because the algorithm relies on knowing that everything below the midpoint is smaller and everything above is larger.

Markers reward the halving argument for O(logn)O(\log n), the linear pass for O(n)O(n), and stating the sorted-data requirement.

Related dot points

Sources & how we know this