TopMyGrade

GCSE/Computer Science/AQA

CS1.4Sorting algorithms: bubble sort and merge sort; tracing each pass and comparing efficiency

Notes

Bubble sort and merge sort

Two sorting algorithms are on the AQA GCSE specification — bubble sort (simple, slow) and merge sort (more complex, much faster on large lists).

Bubble sort — the idea

Repeatedly walk through the list comparing adjacent pairs. Swap any pair that's in the wrong order. After one full pass, the largest item has "bubbled" to the end. Repeat with one fewer item until no swaps occur in a full pass.

SET swapped TO true
WHILE swapped = true
  SET swapped TO false
  FOR i FROM 0 TO LEN(list) - 2
    IF list[i] > list[i+1] THEN
      Swap list[i] and list[i+1]
      SET swapped TO true
    ENDIF
  ENDFOR
ENDWHILE
  • Worst case: $\tfrac{n(n-1)}{2}$ comparisons.
  • Best case (already sorted): $n − 1$ comparisons (one pass with no swaps).
  • Memory: tiny — sorts in place.

Bubble sort trace

Sort [5, 3, 8, 1] ascending:

PassCompareActionList
15,3swap[3, 5, 8, 1]
15,8no swap[3, 5, 8, 1]
18,1swap[3, 5, 1, 8]
23,5no swap[3, 5, 1, 8]
25,1swap[3, 1, 5, 8]
25,8no swap[3, 1, 5, 8]
33,1swap[1, 3, 5, 8]
33,5,8no swaps[1, 3, 5, 8]

Sorted after pass 4 (no swaps).

Merge sort — the idea

A divide-and-conquer algorithm:

  1. Divide the list into halves until each sub-list has 1 item.
  2. Merge pairs of sub-lists in sorted order: compare the fronts, take the smaller one, repeat until both are empty.
FUNCTION mergeSort(list)
  IF LEN(list) <= 1 THEN
    RETURN list
  ENDIF
  SET mid TO LEN(list) DIV 2
  SET left TO mergeSort(list[0:mid])
  SET right TO mergeSort(list[mid:])
  RETURN merge(left, right)
ENDFUNCTION
  • Worst case: $n \log_2 n$ comparisons.
  • Memory: needs extra arrays during merging — not in-place.
  • Reliable performance — the worst case and average case are the same.

Merge sort trace

Sort [5, 3, 8, 1, 9, 2]:

  • Split: [5,3,8] | [1,9,2]
  • Split: [5] [3,8] | [1] [9,2]
  • Split: [5] [3] [8] | [1] [9] [2]
  • Merge: [3,5] [8] | [1] [2,9] (and [8] alone)
  • Merge: [3,5,8] | [1,2,9]
  • Final merge: [1,2,3,5,8,9]

Comparing the two

PropertyBubble sortMerge sort
Easy to codeYesNo (recursion)
In-place?YesNo (needs RAM)
Worst case$n^2$$n \log_2 n$
Best case$n$$n \log_2 n$
Stable?YesYes
Good for big lists?NoYes

For $n = 1000$:

  • Bubble: ~500{,}000 comparisons.
  • Merge: ~10{,}000 comparisons.

For $n = 1{,}000{,}000$:

  • Bubble: 5 × 10¹¹ — minutes/hours.
  • Merge: 2 × 10⁷ — seconds.

When to use bubble sort

Almost never in real code, but it's:

  • Easy to understand and teach.
  • Fast for nearly-sorted small lists (best case is linear).
  • Tiny memory footprint.

Common mistakesPitfalls

  1. Forgetting the early-exit flag in bubble sort — the algorithm keeps going pointlessly through sorted passes.
  2. Off-by-one: FOR i FROM 0 TO LEN(list) − 2 is correct because index $i+1$ is used.
  3. Confusing "passes" and "comparisons" — one pass through a list of $n$ does $n-1$ comparisons.
  4. Forgetting that merge sort uses extra memory (it builds new arrays).

Try thisQuick check

How many comparisons does bubble sort do in the worst case for a list of 6? Answer: $6 \times 5 / 2 = 15$.

How many for merge sort? $6 \log_2 6 ≈ 6 × 2.58 ≈ 15.5$, so on small lists they're similar — merge wins big only as $n$ grows.

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

Practice questions

Try each before peeking at the worked solution.

  1. Question 14 marks

    Bubble-sort trace

    Trace a bubble sort on [4, 2, 1, 3]. Show the list after each pass.

    Ask AI about this

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

  2. Question 24 marks

    Merge-sort splitting

    Show the splitting phase of merge sort applied to [9, 5, 7, 1, 6, 2].

    Ask AI about this

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

  3. Question 34 marks

    Merge-sort merging

    Continue the merge sort from the previous question and show how the sub-lists are merged back into a single sorted list.

    Ask AI about this

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

  4. Question 42 marks

    Worst-case formulas

    Write down the worst-case number of comparisons for (a) bubble sort and (b) merge sort, in terms of $n$.

    Ask AI about this

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

  5. Question 54 marks

    Compare for n = 64

    Estimate the worst-case number of comparisons for sorting 64 items with each algorithm.

    Ask AI about this

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

  6. Question 61 mark

    When bubble might be preferred

    Suggest one reason a programmer might prefer bubble sort over merge sort, despite its worse efficiency.

    Ask AI about this

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

  7. Question 74 marks

    Why merge sort wins on big lists

    Explain why merge sort scales much better than bubble sort to large lists.

    Ask AI about this

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

Flashcards

CS1.4 — Sorting algorithms — bubble sort and merge sort

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

10 cards · spaced repetition (SM-2)