Pro Content

Upgrade to access all Cambridge 9618 lessons including bubble sort, insertion sort and merge sort.

Upgrade to Pro →
← Back to Dashboard
🔢 Paper 2 · 2.4 Algorithms
2.4.2 Sorting Algorithms — Bubble, Insertion & Merge Sort
Cambridge 9618 · International A Level Computer Science · ~16 min read
Notes
Video
Slides
Quiz
Worksheet

Bubble Sort

Bubble sort repeatedly compares adjacent pairs and swaps them if they are in the wrong order. After each pass, the largest unsorted element "bubbles up" to its correct position at the end.

Bubble Sort — Step by Step on [5, 3, 8, 1, 4]

Sorting [5, 3, 8, 1, 4] into ascending order:
Pass 1
3
5
8
1
4
5↔3 swapped; 5<8 no swap; 8↔1 swapped; 8↔4 swapped → 8 in correct place
Pass 2
3
5
1
4
8
3<5 no swap; 5↔1 swapped; 5↔4 swapped → 5 bubbles up
Pass 3
3
1
4
5
8
Pass 4 — no swaps → done
1
3
4
5
8

Bubble Sort — Cambridge 9618 Pseudocode

DECLARE arr : ARRAY[1:5] OF INTEGER
DECLARE i, j, temp : INTEGER
DECLARE swapped : BOOLEAN

FOR i ← 1 TO 4  // n-1 passes
  swapped ← FALSE
  FOR j ← 1 TO 5 - i  // last i elements already sorted
    IF arr[j] > arr[j + 1] THEN
      temp ← arr[j]
      arr[j] ← arr[j + 1]
      arr[j + 1] ← temp
      swapped ← TRUE
    ENDIF
  NEXT j
  IF NOT swapped THEN  // early exit optimisation
    RETURN
  ENDIF
NEXT i

Bubble Sort Complexity

Worst case: O(n²) — already reverse sorted; all n(n-1)/2 comparisons made.
Best case: O(n) — already sorted; one pass with no swaps (with early exit).
Average case: O(n²)

Insertion Sort

Insertion sort builds a sorted list one element at a time. Each element is removed from the unsorted portion and inserted into the correct position in the sorted portion — like sorting playing cards in your hand.

Insertion Sort — Step by Step on [5, 3, 8, 1, 4]

Step 1: key = 3, compare with sorted portion [5]
3
5
8
1
4
3 < 5 → shift 5 right, insert 3 in position 1
Step 2: key = 8, compare with sorted [3, 5]
3
5
8
1
4
8 > 5 → already in correct position
Step 3: key = 1, compare with sorted [3, 5, 8]
1
3
5
8
4
1 < 8, < 5, < 3 → shift all right, insert at start
Step 4: key = 4 → Final sorted array
1
3
4
5
8

Insertion Sort — Cambridge 9618 Pseudocode

DECLARE arr : ARRAY[1:5] OF INTEGER
DECLARE i, j, key : INTEGER

FOR i ← 2 TO 5
  key ← arr[i]  // element to be placed
  j ← i - 1  // start of sorted portion comparison
  WHILE j >= 1 AND arr[j] > key DO
    arr[j + 1] ← arr[j]  // shift element right
    j ← j - 1
  ENDWHILE
  arr[j + 1] ← key  // insert key in correct position
NEXT i

Insertion Sort Complexity

Worst case: O(n²) — reverse sorted; every element must shift past all sorted elements.
Best case: O(n) — already sorted; one comparison per element, no shifts.
Average case: O(n²)

Merge Sort

Merge sort uses a divide and conquer strategy. The array is repeatedly split in half until each subarray has one element (which is trivially sorted), then the subarrays are merged back in sorted order.

Merge Sort — How it Works

// Merge sort uses recursion
PROCEDURE MergeSort(arr, low, high)
  IF low < high THEN
    mid ← (low + high) DIV 2
    MergeSort(arr, low, mid)  // sort left half
    MergeSort(arr, mid + 1, high)  // sort right half
    Merge(arr, low, mid, high)  // merge sorted halves
  ENDIF
ENDPROCEDURE

On array [5, 3, 8, 1]:

  • Split: [5,3] | [8,1]
  • Split again: [5] | [3] | [8] | [1]
  • Merge [5] and [3] → [3,5]
  • Merge [8] and [1] → [1,8]
  • Merge [3,5] and [1,8] → [1,3,5,8]

Merge Sort Complexity

All cases: O(n log₂ n) — always splits and merges the same way regardless of initial order. Uses extra memory for temporary arrays.

Comparison of Sorting Algorithms

AlgorithmBest caseAverage caseWorst caseMemory
Bubble SortO(n)*O(n²)O(n²)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)

*Bubble sort O(n) best case requires the early exit (swapped flag) optimisation.

Exam tip — tracing bubble sort: Cambridge often asks you to show the array after each pass. Key rules: (1) compare adjacent pairs left to right, (2) swap if left > right, (3) after each pass the largest unsorted element is in its final position. Always show the full array state after each pass, not just swaps.
Swap in Cambridge 9618: Always use a temporary variable: temp ← arr[j]; arr[j] ← arr[j+1]; arr[j+1] ← temp. Never write arr[j] ← arr[j+1]; arr[j+1] ← arr[j] — this overwrites arr[j] before it can be saved.
⚠️ Common Mistakes
  • Bubble sort inner loop should go from 1 to (n - i), not 1 to n — avoid comparing already-sorted end elements
  • Swapping without a temp variable — always use a 3-line swap with DECLARE temp
  • Claiming bubble sort is always O(n) — it's only O(n) with early exit on a nearly-sorted array
  • Insertion sort: forgetting to restore the key after the WHILE loop — arr[j+1] ← key is essential
  • Stating merge sort has O(n²) complexity — merge sort is always O(n log n)
  • Forgetting that merge sort needs O(n) extra memory — it creates additional arrays during the merge step
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.4.2 Sorting Algorithms

8 questions · Cambridge 9618 standard

Q1Trace bubble sort on the array [6, 2, 9, 4, 1]. Show the state of the array after each complete pass.[5]
✅ Mark scheme
Pass 1: [2,6,4,1,9] [1]; Pass 2: [2,4,1,6,9] [1]; Pass 3: [2,1,4,6,9] [1]; Pass 4: [1,2,4,6,9] [1]; 4 passes needed [1]. Award marks for correctly showing swaps within each pass.
Q2Write the pseudocode for a 3-line swap of arr[j] and arr[j+1] using a temporary variable.[3]
✅ Mark scheme
temp ← arr[j] [1]; arr[j] ← arr[j+1] [1]; arr[j+1] ← temp [1].
Q3Explain the purpose of the 'swapped' flag in the optimised bubble sort algorithm.[2]
✅ Mark scheme
If no swaps are made during a pass, the array is already sorted [1]; so the swapped flag allows early exit from the outer loop, improving best-case performance to O(n) [1].
Q4Perform one pass of insertion sort on [7, 2, 5, 1, 8]. Show each step of placing the key element '2'.[3]
✅ Mark scheme
key ← 2 (element at position 2) [1]; compare 2 with 7: 2 < 7, shift 7 right to position 2 [1]; no more elements to compare (j=0), insert key at position 1 → [2,7,5,1,8] [1].
Q5Compare bubble sort, insertion sort and merge sort in terms of worst-case time complexity and memory usage.[4]
✅ Mark scheme
Bubble sort: O(n²) worst case, O(1) memory [1]; Insertion sort: O(n²) worst case, O(1) memory [1]; Merge sort: O(n log n) worst case — consistent for all inputs [1]; Merge sort uses O(n) extra memory for temporary arrays; bubble/insertion sort sort in-place [1].
Q6A programmer has a nearly-sorted array of 10,000 integers and needs to sort it quickly. Which algorithm would you recommend and why?[3]
✅ Mark scheme
Insertion sort [1]; for a nearly-sorted array, insertion sort approaches O(n) because each element needs very few comparisons/shifts [1]; bubble sort with early exit also performs well on nearly-sorted data [1]. Merge sort would be O(n log n) regardless, which is slower than O(n) for nearly-sorted data. (Award 3 max.)
Q7Write a FUNCTION called MaxOfThree that takes three INTEGER parameters and returns the largest value. Write a PROCEDURE called PrintGrade that takes an INTEGER mark and outputs "Pass" if ≥ 50, "Merit" if ≥ 70, otherwise "Fail". Explain why MaxOfThree is a FUNCTION and PrintGrade is a PROCEDURE.[6]
✅ Mark scheme
FUNCTION MaxOfThree(a,b,c : INTEGER) RETURNS INTEGER — 1 mark; IF a > b AND a > c THEN RETURN a ELSIF b > c THEN RETURN b ELSE RETURN c — 1 mark; PROCEDURE PrintGrade(mark : INTEGER) — 1 mark; IF mark ≥ 70 THEN OUTPUT "Merit" ELSIF mark ≥ 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" — 1 mark; FUNCTION returns a value used in an expression — 1 mark; PROCEDURE performs actions without returning a value — 1 mark.
Q8Explain what is meant by passing a parameter by value versus by reference. Give a pseudocode example showing both, and explain which is appropriate for a FUNCTION that calculates tax on a salary without changing the salary.[5]
✅ Mark scheme
By value: a copy of the argument is passed; changes inside the procedure do not affect original — 1 mark; by reference: the address is passed; changes inside affect the original variable — 1 mark; BYREF shown with keyword BYREF or &: PROCEDURE Swap(BYREF a,b : INTEGER) — 1 mark; tax calculation should use BYVAL (default) since salary must not be modified — 1 mark; FUNCTION CalcTax(salary : REAL) RETURNS REAL — 1 mark.
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 6
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 2.4.2 Sorting Algorithms

10 questions · 10 marks · 10 minutes

← 2.4.1 Searching
44 of 82 · Cambridge 9618
2.4.3 ADT Implementation →