TopMyGrade

GCSE/Computer Science/AQA

CS1.3Searching algorithms: linear search and binary search; required preconditions and step-by-step traces

Notes

Linear and binary search

Two search algorithms are on the AQA spec: linear search and binary search. Both find a target item in a list, but they have very different costs.

Linear search

Walk through the list from the start, checking each item until you find the target or reach the end.

SET found TO false
SET i TO 0
WHILE i < LEN(list) AND found = false
  IF list[i] = target THEN
    SET found TO true
  ENDIF
  SET i TO i + 1
ENDWHILE
  • Works on any list — sorted or unsorted.
  • Best case: target is the first item — 1 comparison.
  • Worst case: target is last or absent — $n$ comparisons.
  • Average: $n/2$ comparisons.

Binary search

Only works on a sorted list. Idea: check the middle; if it's the target, done. If the target is smaller, search the left half; otherwise search the right half. Repeat.

SET low TO 0
SET high TO LEN(list) - 1
SET found TO false
WHILE low <= high AND found = false
  SET mid TO (low + high) DIV 2
  IF list[mid] = target THEN
    SET found TO true
  ELSEIF list[mid] < target THEN
    SET low TO mid + 1
  ELSE
    SET high TO mid - 1
  ENDIF
ENDWHILE
  • Best case: 1 comparison (item is the middle).
  • Worst case: $\lceil \log_2 n \rceil$ comparisons.
  • For $n = 1024$ that's 10. For $n = 1{,}048{,}576$ it's 20.

Step-by-step trace — binary search

Find 22 in [3, 7, 11, 15, 18, 22, 30, 41]: $n = 8$.

Steplowhighmidlist[mid]Action
10731522>15: low=4
247522found!

Found in 2 comparisons vs up to 8 for linear.

When to use which

  • Linear search — small lists, unsorted data, simple code.
  • Binary search — large sorted lists; the cost of sorting first is amortised over many searches.

Common pitfalls

  1. Trying binary search on an unsorted list — it doesn't work.
  2. Off-by-one with high (must be len-1, not len).
  3. Forgetting to update low/high — infinite loop.
  4. Calculating mid as low + high (overflow on big lists at A-Level; safe at GCSE since DIV 2 works fine).
  5. Returning the wrong index when the target appears more than once. (Both algorithms find one match.)

Worked exampleWorked example — linear search

Find 7 in [4, 9, 7, 12, 1].

  • i=0 → 4? no.
  • i=1 → 9? no.
  • i=2 → 7? yes — return index 2.
  • 3 comparisons.

Why binary search is so fast

Each step halves the remaining range. After $k$ steps, the range is $n / 2^k$. The search ends when the range shrinks to 1, so $2^k = n$, i.e. $k = \log_2 n$. Doubling the list adds one more step — that's the magic.

Required preconditions and trade-offs

AlgorithmSorted list?BestAverageWorstMemory
LinearNo1$n/2$$n$tiny
BinaryYes1$\log n$$\log n$tiny

If the list will be searched many times, sort it once and use binary search. If you only search once, sorting first costs more than just doing a linear pass.

Trace tip for the exam

Examiners love asking you to trace binary search and write down low, high, mid and list[mid] at each step. Always make a table — it stops you losing track and the examiner can give partial credit.

AI-generated · claude-opus-4-7 · v3-deep-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 14 marks

    Linear search trace

    Trace a linear search for the value 14 in the list [9, 12, 14, 7, 5]. State the number of comparisons.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  2. Question 24 marks

    Binary search trace

    The list [2, 5, 8, 11, 14, 17, 20] is sorted. Use a table with columns low, high, mid, list[mid] to trace a binary search for 14.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  3. Question 32 marks

    Precondition

    State the precondition that must hold for binary search to work. Justify.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  4. Question 43 marks

    Worst case for n = 1024

    For a sorted list of 1024 items: (a) Worst-case number of comparisons for linear search? (b) Worst-case for binary search? Show working for (b).

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  5. Question 52 marks

    When linear beats binary

    Suggest two reasons a programmer might still choose linear search over binary search.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  6. Question 64 marks

    Searching for absent item

    Trace a binary search for 9 in [1, 4, 6, 8, 10, 12, 15]. State the number of comparisons before reporting "not found".

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

  7. Question 73 marks

    Doubling effect

    A binary search on a 1000-item list takes a worst case of 10 comparisons. The list grows to 2000 items. Explain how many comparisons the worst case requires.

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-deep-computer-science

Flashcards

CS1.3 — Searching algorithms — linear and binary search

10-card SR deck for AQA GCSE Computer Science topic CS1.3

10 cards · spaced repetition (SM-2)