Skip to main content
EnglandComputer ScienceSyllabus dot point

How do linear and binary search work, and why is binary search so much faster on a sorted list?

Searching algorithms: linear search, binary search and binary tree search, how each works with a trace, the precondition for binary search, and their time complexity in Big-O notation.

An OCR H446 answer on searching algorithms: how linear search, binary search and binary tree search work with worked traces, the requirement that binary search needs a sorted list, and the time complexity of each in Big-O notation.

Generated by Claude Opus 4.814 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. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

OCR wants you to describe and trace linear search, binary search and binary tree search, state the precondition for binary search, and give each algorithm's time complexity in Big-O. This is trace-table and complexity territory: expect to complete a trace and to compare complexities.

The answer

Linear search

function linearSearch(list, target)
  for i = 0 to length(list) - 1
    if list[i] == target then
      return i
    endif
  next i
  return -1   // not found
endfunction

Binary search

Binary tree search

Examples in context

A dictionary or phone book lookup is naturally a binary search, you open in the middle and discard half. Databases index data in balanced trees (B-trees) so lookups are logarithmic. Linear search is used where data is small, unsorted or searched once (sorting first would not pay off). The choice illustrates the trade-off OCR stresses: sorting costs time but enables fast repeated binary searches. This links directly to sorting algorithms and to Big-O analysis.

Try this

Q1. State the precondition that must hold before binary search can be used. [1 mark]

  • Cue. The list must be sorted.

Q2. State the worst-case time complexity of linear search and of binary search. [2 marks]

  • Cue. Linear search O(n)O(n); binary search O(logn)O(\log n).

Q3. Explain why binary tree search can degrade to O(n)O(n). [2 marks]

  • Cue. If the tree is unbalanced (for example built from sorted data into a long chain), its height approaches nn, so a search may have to descend nn nodes.

Exam-style practice questions

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

OCR 20196 marksA sorted list contains [3,7,11,15,19,23,27][3, 7, 11, 15, 19, 23, 27] (indices 0 to 6). Using a trace table, show how a binary search finds the value 19, and state the time complexity of binary search.
Show worked answer →

Trace (up to 5):

low high mid list[mid] comparison
0 6 3 15 15<1915 < 19, so search right; low = 4
4 6 5 23 23>1923 > 19, so search left; high = 4
4 4 4 19 found at index 4

Each step computes mid=(low+high) DIV 2mid = (low + high)\ \text{DIV}\ 2, compares, and halves the range. The value 19 is found at index 4 after three comparisons.

Complexity (1 mark): binary search is O(logn)O(\log n) because the search space halves each step. Markers reward a correct trace table with mid recalculated and the range narrowing, and the O(logn)O(\log n) complexity.

OCR 20216 marksExplain why linear search has time complexity O(n)O(n) while binary search has O(logn)O(\log n), and explain the condition that must hold before binary search can be used.
Show worked answer →

Linear search O(n)O(n) (up to 2): linear search checks each item in turn from the start; in the worst case (item last or absent) it examines all nn items, so the number of comparisons grows in proportion to nn.

Binary search O(logn)O(\log n) (up to 2): binary search compares the target with the middle item and discards half the remaining items each step; the number of steps needed to reduce nn items to one is log2n\log_2 n, so the work grows logarithmically.

Condition (up to 2): binary search requires the list to be sorted, because it relies on knowing that everything to one side of the middle is smaller (or larger) than the target. On an unsorted list it cannot decide which half to discard. Markers reward "checks all nn in the worst case" for linear, "halves each step" for binary, and the sorted precondition.

Related dot points

Sources & how we know this