📄 Paper 1 · 4.3 Algorithms
⭐ Pro
4.3.5 Sorting Algorithms
AQA 7517 · A-Level Computer Science · ~20 min read

Sorting Algorithms

AQA 7517 requires knowledge of bubble sort, insertion sort, merge sort, and quicksort. You must be able to trace and analyse each.

Bubble Sort

Repeatedly steps through the list, comparing adjacent elements and swapping if they are in the wrong order. After each pass, the largest unsorted element "bubbles" to its correct position.

// Bubble sort — O(n²)
PROCEDURE bubbleSort(arr)
  n ← len(arr)
  FOR i ← 0 TO n-2
    FOR j ← 0 TO n-2-i
      IF arr[j] > arr[j+1] THEN
        SWAP arr[j], arr[j+1]
      END IF
    END FOR
  END FOR
END PROCEDURE

// Trace: [5, 3, 8, 1]
// Pass 1: 3,5,8,1 → 3,5,1,8  (8 placed)
// Pass 2: 3,1,5,8             (5 placed)
// Pass 3: 1,3,5,8             (sorted)

Insertion Sort

Builds a sorted sublist one element at a time by taking each element and inserting it into its correct position among the already-sorted elements.

// Insertion sort — O(n²) worst, O(n) best (already sorted)
PROCEDURE insertionSort(arr)
  FOR i ← 1 TO len(arr)-1
    key ← arr[i]
    j ← i - 1
    WHILE j >= 0 AND arr[j] > key
      arr[j+1] ← arr[j]   // Shift right
      j ← j - 1
    END WHILE
    arr[j+1] ← key         // Insert
  END FOR
END PROCEDURE

// Trace: [5, 3, 8, 1]
// i=1: key=3, shift 5 right → [3,5,8,1]
// i=2: key=8, no shift → [3,5,8,1]
// i=3: key=1, shift 8,5,3 → [1,3,5,8]

Merge Sort

Divide and conquer — recursively split the array in half until single elements, then merge sorted halves back together.

// Merge sort — O(n log n) always
PROCEDURE mergeSort(arr)
  IF len(arr) <= 1 THEN RETURN arr
  mid ← len(arr) DIV 2
  left ← mergeSort(arr[0..mid-1])
  right ← mergeSort(arr[mid..])
  RETURN merge(left, right)
END PROCEDURE

// merge: compare fronts, take smaller, repeat
// Trace: [5,3,8,1]
// Split: [5,3] and [8,1]
// Split: [5],[3] and [8],[1]
// Merge: [3,5] and [1,8]
// Merge: [1,3,5,8] ✓

Quicksort

Divide and conquer — choose a pivot, partition into elements less than and greater than pivot, recursively sort each partition.

// Quicksort — O(n log n) avg, O(n²) worst (bad pivot)
PROCEDURE quicksort(arr, low, high)
  IF low < high THEN
    p ← partition(arr, low, high)
    quicksort(arr, low, p-1)
    quicksort(arr, p+1, high)
  END IF
END PROCEDURE

// Trace: [5,3,8,1,4] pivot=5 (first element)
// Partition: [3,1,4] | 5 | [8]
// Recurse left: [3,1,4] pivot=3 → [1] | 3 | [4]
// Result: [1,3,4,5,8] ✓

Comparison Table

AlgorithmBestAverageWorstSpaceStable?
Bubble sortO(n)O(n²)O(n²)O(1)Yes
Insertion sortO(n)O(n²)O(n²)O(1)Yes
Merge sortO(n log n)O(n log n)O(n log n)O(n)Yes
QuicksortO(n log n)O(n log n)O(n²)O(log n)No
Exam tip: Know that merge sort is always O(n log n) — it guarantees this even in the worst case, unlike quicksort. Quicksort's worst case is O(n²) when the pivot is always the smallest or largest element (already sorted data with first-element pivot). Bubble and insertion sort are O(n²) average but O(n) on nearly-sorted data. A stable sort preserves the relative order of equal elements.
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.3.5 Sorting Algorithms

8 questions · instantly marked · AQA 7517 standard

Q1State the worst-case time complexity of bubble sort, merge sort, and quicksort.[3]
✅ Mark scheme
Mark scheme
Bubble sort: O(n²) [1]; Merge sort: O(n log n) [1]; Quicksort: O(n²) [1].
Q2Trace bubble sort on the array [4, 2, 7, 1, 5]. Show the state of the array after each pass.[4]
✅ Mark scheme
Mark scheme
Start: [4,2,7,1,5]; Pass 1: [2,4,1,5,7] [1]; Pass 2: [2,1,4,5,7] [1]; Pass 3: [1,2,4,5,7] [1]; Pass 4: [1,2,4,5,7] sorted [1].
Q3Why is merge sort guaranteed O(n log n) in all cases, while quicksort can degrade to O(n²)?[3]
✅ Mark scheme
Mark scheme
Merge sort always divides the array exactly in half, ensuring log n levels of recursion [1]; quicksort's partitioning depends on the pivot choice — a bad pivot (e.g. always the smallest) creates one empty partition and one of size n-1, leading to n levels of recursion [1]; merge sort is therefore O(n log n) always, quicksort only on average [1].
Q4Show the merge sort splitting and merging steps for the array [6, 2, 8, 3].[4]
✅ Mark scheme
Mark scheme
Split: [6,2] and [8,3] [1]; Split further: [6],[2] and [8],[3]; Merge: [2,6] and [3,8] [1]; Final merge: [2,3,6,8] [2].
Q5In which scenario does insertion sort outperform both merge sort and quicksort?[2]
✅ Mark scheme
Mark scheme
When the data is nearly sorted / already sorted [1]; insertion sort's best case is O(n) as only one pass is needed — much better than the O(n log n) constant overhead of merge/quicksort [1].
Q6What is a 'stable' sort? Which of the four sorting algorithms covered in this lesson are stable?[3]
✅ Mark scheme
Mark scheme
A stable sort preserves the relative order of equal elements [1]; bubble sort and insertion sort are stable [1]; merge sort is also stable; quicksort is generally NOT stable [1].
Q7Trace quicksort on [3, 6, 1, 4, 2] using the first element as pivot for the first partition. Show the partition result.[3]
✅ Mark scheme
Mark scheme
Pivot = 3; elements < 3: [1,2]; elements > 3: [6,4] [1]; partition result: [1,2] | 3 | [6,4] [1]; recursively sort subarrays to get [1,2,3,4,6] [1].
Q8A large dataset (10 million items) needs sorting once. Which algorithm would you recommend and why?[3]
✅ Mark scheme
Mark scheme
Merge sort or quicksort (with good pivot selection) recommended [1]; both are O(n log n) average — far faster than O(n²) sorts for large n [1]; merge sort preferred if guaranteed O(n log n) is required; quicksort preferred if in-place sorting (less memory) is needed [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Sorting

10 questions · 10 minutes

← 4.3.4 Searching
23 of 70 · AQA 7517
4.3.6 Dijkstra's Algorithm →