Skip to main content
EnglandComputer ScienceSyllabus dot point

How do linear search and binary search work, and when is each one appropriate?

Standard searching algorithms: linear search and binary search, how each works step by step, the requirement that binary search needs a sorted list, and the comparison of their efficiency.

An OCR J277 2.1.3 answer on the two standard searching algorithms: how linear search and binary search work step by step, why binary search needs a sorted list, and how their efficiency compares.

Generated by Claude Opus 4.810 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 efficiency
  5. Try this

What this dot point is asking

OCR wants you to describe the two standard searching algorithms, linear search and binary search, step by step, to know that binary search needs a sorted list, and to compare their efficiency. You must be able to trace each on a given list and say which is appropriate for a situation. This is examined in Paper 2, often as a description or a comparison.

In OCR Exam Reference Language, a linear search of a list data for a target:

found = false
i = 0
while i < data.length AND found == false
  if data[i] == target then
    found = true
    position = i
  endif
  i = i + 1
endwhile

Comparing efficiency

The key comparison is how the work grows as the list gets bigger. Linear search checks items one by one, so doubling the list size roughly doubles the work in the worst case. Binary search halves the remaining items each step, so doubling the list size adds only one extra step. On a list of a million items, linear search may make up to a million comparisons, while binary search makes about twenty. The trade-off is that binary search needs the list to be sorted first, which takes time, so for a small list, an unsorted list, or a one-off search, linear search can be the better choice.

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. In the worst case, how many items does a linear search check in a list of 50 items? [1 mark]

  • Cue. 50 (it may have to check every item).

Q3. Explain why binary search is faster than linear search on a large sorted list. [2 marks]

  • Cue. Binary search discards half the remaining items at each step, so the number of items left shrinks very quickly, whereas linear search checks items one at a time.

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 20214 marksDescribe how a binary search would find the value 23 in the sorted list 4, 8, 15, 16, 23, 42. State the value examined at each step.
Show worked answer →

Binary search repeatedly checks the middle item and discards half the list. The list has 6 items (indexes 0 to 5).

Step 1: the middle is taken (index 2 or the lower middle of 2 and 3). Checking index 2 gives 15. 23 > 15, so discard the left half and search 16, 23, 42 (indexes 3 to 5).

Step 2: the middle of indexes 3 to 5 is index 4, which is 23. This matches, so the search succeeds and returns the position.

Markers reward the method (check the middle, compare, discard the half that cannot contain the target, repeat) and the correct values examined. Stating that the list must be sorted for binary search to work is often credited.

OCR 20226 marksCompare linear search and binary search. Your answer should explain how each works and discuss their relative efficiency and when each is appropriate.
Show worked answer →

A 6-mark question, so a structured comparison, not two separate descriptions.

Linear search: checks each item in turn from the start until it finds the target or reaches the end. It works on any list, sorted or not, but is slow on large lists because in the worst case it checks every item.

Binary search: requires a sorted list. It checks the middle item, and because the list is sorted it can discard the half that cannot contain the target, repeating on the remaining half. It is much faster on large lists because each step halves the number of items left.

Comparison: binary search is far more efficient on large sorted lists (it halves the search each time), but it only works if the list is sorted, and sorting first has a cost. Linear search is simpler and works on unsorted or small lists. Markers reward how each works, the sorted-list requirement, and a clear efficiency comparison with a conclusion on when each is appropriate.

Related dot points

Sources & how we know this