Home / GCSE · AQA / Computer Science / Algorithms

CS3Searching and Sorting Algorithms

3.1.3 Searching algorithms

Comparing linear and binary search

  • If a list changes often, keeping it sorted for binary search costs time. A linear search can be used straight away.
  • For a short list, the difference in time is tiny, so the simpler linear search is fine.
Linear searchBinary search
Does the list need sorting?No: works on unsorted dataYes: must be sorted first
Comparisons on a large listUp to one per itemFar fewer: each comparison removes about half of what’s left
CodingSimpler to writeMore complex (low, high and mid to track)
Best forSmall or unsorted lists, or lists that change oftenLarge sorted lists that are searched often
Items in a sorted listLinear search: most comparisonsBinary search: most comparisons
10104
1001007
1000100010
1 000 0001 000 00020
Exam tipDon’t just write “binary search is faster”. Say why: “binary search makes fewer comparisons on a large sorted list, because each comparison halves the items left, so it takes less time.”
3.1.4 Sorting algorithms

Bubble sort

AQA: a bubble sort works by making passes through the array or list. Whilst making a pass, we compare neighbours and swap them if they are out of order. After a series of passes, the array or list will be sorted.

  • A pass compares items 0 and 1, then 1 and 2, and so on to the end.
  • If a pair is out of order, swap them. A swap needs a temporary variable, so one value isn’t overwritten before it is copied.
  • After the first pass (ascending order), the largest item is in its final place at the end. After each pass, one more item is in place.
  • Stopping: a list of n items is sorted after at most n − 1 passes. Most versions stop as soon as a pass makes no swaps, because then the list must be in order.
AQA pseudo-code: bubble sort with an early stop
swapped ← True
WHILE swapped = True
   swapped ← False
   FOR i ← 0 TO LEN(items) - 2
      IF items[i] > items[i + 1] THEN
         temp ← items[i]
         items[i] ← items[i + 1]
         items[i + 1] ← temp
         swapped ← True
      ENDIF
   ENDFOR
ENDWHILE

The first pass: the largest item “bubbles” to the end

Bubble sort
A sort that makes passes through a list, comparing neighbours and swapping them if they are out of order, until the list is sorted.
Pass
One run through the list, comparing each pair of neighbours once.
Swap
Exchanging two items, using a temporary variable to hold one of them.
3.1.4 Sorting algorithms

Showing the steps of a bubble sort

AQA often asks you to show the list each time it changes, which means after every swap, not just after every pass.

Worked exampleBubble sort [5, 1, 4, 2] into ascending order. Show the list after each swap.
  1. Pass 1: 5 > 1 swap → [1, 5, 4, 2]; 5 > 4 swap → [1, 4, 5, 2]; 5 > 2 swap → [1, 4, 2, 5]three swaps; 5 is now in place
  2. Pass 2: 1 < 4 no swap; 4 > 2 swap → [1, 2, 4, 5]; 4 < 5 no swapone swap
  3. Pass 3: no swapsso the sort stops
Four changes: [1, 5, 4, 2], [1, 4, 5, 2], [1, 4, 2, 5], [1, 2, 4, 5].
Worked exampleSort ['owl', 'bat', 'elk'] into alphabetical order with a bubble sort. Show each change.
  1. 'owl' > 'bat' alphabetically: swap → ['bat', 'owl', 'elk']b comes before o
  2. 'owl' > 'elk': swap → ['bat', 'elk', 'owl']e comes before o
  3. Pass 2: 'bat' < 'elk', 'elk' < 'owl': no swapssorted
Two changes. Words compare letter by letter, from the first letter.
Exam tipIn June 2025, about a third of students missed full marks here, often by showing only the end of each pass. Write a new line for every swap.
3.1.4 Sorting algorithms

Merge sort

AQA: the merge sort is a ‘divide-and-conquer’ algorithm. The original array or list is broken down into smaller and smaller parts until we have individual elements. The merge sort continues by building back sorted groups until we end with the full sorted result.

  • Split the list in half, then split each half, until every list holds one item. A list of one item is already sorted.
  • Merge pairs of lists back together in order. Keep merging until there is one list.
  • Merging and sorting are the same step. Each merge produces a sorted list, because items are taken in order.
  • If a list has an odd number of items, one half gets one more item than the other.
Worked exampleMerge the sorted lists [4, 9, 15] and [6, 7, 20].
  1. Fronts 4 and 6: take 4 → [4]always compare the two front items
  2. 9 and 6: take 6 → [4, 6]. 9 and 7: take 7 → [4, 6, 7]take the smaller each time
  3. 9 and 20: take 9. 15 and 20: take 15 → [4, 6, 7, 9, 15]the first list is now empty
  4. Copy the rest of the other list: 20nothing left to compare
[4, 6, 7, 9, 15, 20]
Merge sort
A divide-and-conquer sort: the list is broken down until there are individual elements, then sorted groups are built back up until the whole list is sorted.
Merge
Combine two sorted lists into one sorted list by repeatedly taking the smaller front item.
Divide and conquer
Solving a problem by splitting it into smaller problems, solving those and combining the results.
3.1.4 Sorting algorithms

Drawing a merge sort

  • Draw a new row for each round of splitting, until every box holds one item.
  • Then draw a new row for each round of merging. Each merge joins two neighbouring lists from the row above, in order.
  • A list of 8 items takes 3 rounds of splitting and 3 rounds of merging.

Split down to single items, then merge back up in order

Worked exampleShow the stages of a merge sort on [31, 12, 8, 25, 17, 4].
  1. Split: [31, 12, 8] [25, 17, 4]half each
  2. Split: [31] [12, 8] [25] [17, 4], then [31] [12] [8] [25] [17] [4]odd-length lists split unevenly
  3. Merge: [8, 12] and [4, 17]; then [8, 12, 31] and [4, 17, 25]merge back in the reverse order of splitting
  4. Merge: [4, 8, 12, 17, 25, 31]the last merge
[4, 8, 12, 17, 25, 31]
Exam tipFewer than 40% of students got full marks for completing a merge diagram in June 2025. Many treated merging and sorting as two separate steps, merging lists without putting the items in order.
3.1.4 Sorting algorithms

Comparing merge sort and bubble sort

  • For a large, unsorted list, merge sort is the better choice: it takes far less time.
  • For a small or nearly sorted list, or where memory is tight, bubble sort can be the better choice.
Bubble sortMerge sort
Time on a large listSlow: at least 499 500 comparisons for 1000 items in reverse orderMuch quicker: fewer than 10 000 comparisons for 1000 items
Nearly sorted listQuick: with the early stop, one pass can be enoughStill splits and merges everything
MemorySorts in place: needs only one extra variable for swapsNeeds extra memory for the lists it builds while merging
CodingSimple to writeMore complex to write
Exam tip“Bubble sort is simple” isn’t enough. Qualify it: “bubble sort is simpler to code” or “bubble sort uses less memory because it sorts in place”.
3.1.3 Searching algorithms

Writing a linear search in Python

Paper 1 may ask you to write your own search. When it says so, don’t use Python’s built-in shortcuts such as if target in names or names.index(target).

  • i starts at 0, and i < len(names) stops the loop before it runs off the end.
  • The loop stops as soon as the name is found, or when every item has been checked.
  • The result is output once, after the loop.
Python: a linear search that stops when it finds a match
names = ["Ola", "Kit", "Raj", "Bea", "Tom"]
target = input("Name to find: ")
found = False
i = 0
while i < len(names) and not found:
    if names[i] == target:
        found = True
    else:
        i = i + 1
if found:
    print(f"{target} is at position {i}")
else:
    print(f"{target} is not in the list")
Exam tipThe commonest errors: printing “not found” inside the loop (so it appears once per item), indexes that run past the end, and using in or index() when the question asked for your own search.