GCSE · Algorithms

Algorithms, searching and sorting

Free worked examples, independent practice and self-check answers.

What makes an algorithm?

An algorithm is a finite, clearly defined sequence of steps for solving a problem. A good algorithm has clear inputs, outputs and termination.

Linear search

Linear search checks one item at a time. It works on unsorted data. In the worst case it checks all n items: O(n).

def linear_search(values, target):
    for i in range(len(values)):
        if values[i] == target:
            return i
    return -1

Binary search

Binary search repeatedly halves the search interval, but requires a sorted list. Its worst-case time is O(log n).

Bubble sort

Adjacent values are compared and swapped if out of order. Repeated passes gradually move larger values to the end; worst-case O(n²).

Practise

Trace linear search for 7 in [3, 9, 2, 7, 5]. How many comparisons occur? Explain why binary search cannot reliably be used on this unsorted list.

Suggested answer

Four comparisons; binary search relies on ordered values so each half can be safely discarded.

Practise tracing and comparing algorithms

Trace linear search on an unsorted list, then try binary search on a sorted version of the same list. Explain why binary search must use ordered data. Work through one bubble-sort pass and record each swap before attempting the corresponding GCSE Python lessons.

Algorithm tracing: how to revise the method

When predicting an algorithm’s output, create a trace table with columns for each variable, the loop condition and any output. Update values only when the corresponding statement executes. This is more reliable than trying to keep all changes in your head.

For searches and sorts, practise identifying the algorithm from the sequence of steps, but also explain why the method works and when it is suitable. Binary search, for example, relies on the data being sorted.

Try without notes: A linear search examines 8, 3, 12, 5 for the target 12. How many elements are compared?

Check an example answer and feedback

Three elements: 8, then 3, then 12. The search stops when the target is found.

Continue with OCR programming revision or AQA programming revision.

ORIGINAL TUTORSYNTAX PRACTICE

GCSE exam practice: choose the search

A list contains unsorted values [9, 2, 14, 6, 11]. Explain which search method can reliably locate 6 without preprocessing and how it works. [3 marks]

Reveal indicative marking points and explanation
  1. Linear search can work on an unsorted list.
  2. Compare the target with the items in order until a match is found.
  3. The search finds 6 at the fourth checked item and stops.

These are original teaching points for self-assessment, not an official exam-board mark scheme. Credit valid alternative explanations.

Take it further: Now sort the values and trace binary search for 6, recording the middle value on every iteration.

Keep learning

Related TutorSyntax activities

Continue with a practical task that builds directly on this topic.