Lesson 6 of 8
Algorithms: Lesson 6

Bubble Sort

The classic comparison-based sorting algorithm. Large values "bubble up" to the right in each pass. Learn the standard algorithm, trace it pass by pass, and understand the early termination optimisation that can make it far more efficient.

50 minutes Animated bubble sort visualiser
Pseudocode style: Saved automatically

Sorting is one of the most fundamental operations in computing. Searching is faster on sorted data. Merging two lists is easier when both are sorted. Presenting results in alphabetical or numerical order requires sorting. Bubble sort is the simplest sorting algorithm to understand, trace and describe - which is why it appears frequently in exam questions. It is rarely used in production software (merge sort and quicksort are far faster for large lists), but mastering bubble sort builds the intuition you need for all other sorting algorithms.

Think about it: You have five cards face down, each with a number, and you can only look at two adjacent cards at a time. How would you sort them into order? You would compare neighbours and swap them if they are out of order - then repeat. How many passes through the cards do you think you need? Can you ever finish early?
Terms you need to know
Bubble sort
A sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order, causing large values to "bubble up" to the end.
Pass
One complete iteration through the list, comparing all adjacent pairs. After each pass, at least one more element is in its final sorted position.
Swap
Exchanging the positions of two adjacent elements. A temporary variable (temp) is used to prevent data loss during the exchange.
Early termination
An optimisation that stops the sort immediately if a full pass completes with no swaps - the list is already sorted.
n-1 passes maximum
A list of n items requires at most n-1 passes to be fully sorted. After n-1 passes, all elements are in their correct positions.
Worst case
O(n2) comparisons - occurs when the list is in reverse order. n-1 passes, each making n-1, n-2, ... 1 comparisons respectively.
The algorithm explained

Bubble sort works in passes. In each pass, it compares every adjacent pair of elements and swaps them if the left element is greater than the right. After each pass, the largest unsorted element has "bubbled" to its correct position at the right.

After pass 1, the largest element is at the last position. After pass 2, the second-largest is at the second-to-last position. After n-1 passes, all elements are sorted.

Swap mechanism

To swap two elements without losing data, a temporary variable (temp) is used:

// Swap myList[i] and myList[i+1] using a temp variable
temp  myList[i]        // Save the first value
myList[i]  myList[i+1]   // Overwrite first with second
myList[i+1]  temp         // Overwrite second with saved first
# Swap myList[i] and myList[i+1]
temp = myList[i]
myList[i] = myList[i+1]
myList[i+1] = temp
Why a temp variable is needed

Without temp: if you write myList[i] = myList[i+1] first, the original value of myList[i] is overwritten and permanently lost. You can no longer assign it to myList[i+1]. The temp variable stores the first value safely while the assignment takes place.

Writing bubble sort
// Bubble sort - sorts myList into ascending order (AQA)
SUBROUTINE bubbleSort(myList)
    n  LEN(myList)
    FOR pass  1 TO n - 1
        FOR i  0 TO n - pass - 1
            IF myList[i] > myList[i+1] THEN
                temp  myList[i]
                myList[i]  myList[i+1]
                myList[i+1]  temp
            ENDIF
        ENDFOR
    ENDFOR
    RETURN myList
ENDSUBROUTINE
# Bubble sort - sorts myList into ascending order (OCR)
procedure bubbleSort(myList)
    n = len(myList)
    for pass = 1 to n - 1
        for i = 0 to n - pass - 1
            if myList[i] > myList[i+1] then
                temp = myList[i]
                myList[i] = myList[i+1]
                myList[i+1] = temp
            end if
        next i
    next pass
    return myList
endprocedure
Why the inner loop goes to n - pass - 1

After pass 1, the largest element is correctly placed at position n-1. After pass 2, the second-largest is at n-2. So with each pass, one fewer comparison is needed. The inner loop boundary n - pass - 1 avoids comparing already-sorted elements, saving a small amount of unnecessary work.

Pass-by-pass trace

Trace bubble sort on [64, 25, 12, 22, 11]:

PassAfter passSwaps made?
Start[64, 25, 12, 22, 11]-
Pass 1[25, 12, 22, 11, 64]Yes (64 bubbles to end)
Pass 2[12, 22, 11, 25, 64]Yes
Pass 3[12, 11, 22, 25, 64]Yes
Pass 4[11, 12, 22, 25, 64]Yes - all sorted

Green = confirmed in final position. 4 passes (n-1 = 5-1) for a 5-element list.

Pass 1 detail - comparing every adjacent pair:

CompareValuesSwap?List after
i=0: [0] vs [1]64 vs 25Yes (64>25)[25, 64, 12, 22, 11]
i=1: [1] vs [2]64 vs 12Yes[25, 12, 64, 22, 11]
i=2: [2] vs [3]64 vs 22Yes[25, 12, 22, 64, 11]
i=3: [3] vs [4]64 vs 11Yes[25, 12, 22, 11, 64]

64 "bubbled up" to the end in one pass. 4 comparisons made in pass 1.

Exam angle

Exam questions on bubble sort commonly ask you to: (1) Show the list after each pass. (2) State how many comparisons a specific pass makes. (3) Identify when the algorithm would terminate early. Always show the swap using a temp variable when writing the algorithm. Know that after pass k, the last k elements are in their correct final positions.

The optimisation that can make bubble sort much faster

Standard bubble sort always runs n-1 passes regardless of whether the list is already sorted. The early termination optimisation uses a boolean flag (swapped): if a complete pass makes no swaps at all, the list must already be sorted - stop immediately.

// Bubble sort with early termination (AQA)
SUBROUTINE bubbleSortOptimised(myList)
    n  LEN(myList)
    swapped  TRUE
    pass  0
    WHILE swapped = TRUE
        swapped  FALSE    // Assume sorted this pass
        FOR i  0 TO n - pass - 2
            IF myList[i] > myList[i+1] THEN
                temp  myList[i]
                myList[i]  myList[i+1]
                myList[i+1]  temp
                swapped  TRUE    // A swap was made
            ENDIF
        ENDFOR
        pass  pass + 1
    ENDWHILE
    RETURN myList
ENDSUBROUTINE
# Bubble sort with early termination (OCR)
procedure bubbleSortOptimised(myList)
    n = len(myList)
    swapped = true
    pass = 0
    while swapped == true
        swapped = false
        for i = 0 to n - pass - 2
            if myList[i] > myList[i+1] then
                temp = myList[i]
                myList[i] = myList[i+1]
                myList[i+1] = temp
                swapped = true
            end if
        next i
        pass = pass + 1
    end while
    return myList
endprocedure
Best case with early termination

With early termination, if the list is already sorted, only one pass is needed (to confirm no swaps are necessary). Without early termination, bubble sort would still run n-1 passes even on a sorted list. This makes the best case O(n) with early termination, compared to O(n2) without it.

Watch bubble sort in action

Press Sort to watch the animated bubble sort. Orange bars are being compared. Red bars are being swapped. Green bars are in their final sorted position.

Press Sort to begin, or Shuffle to change the list.

Test yourself

1. After the first complete pass of bubble sort on [5, 3, 8, 1, 9, 2], which element is guaranteed to be in its final sorted position?

After the first pass, the largest element (9) has bubbled to the rightmost position. It will not be moved again. This is why the inner loop boundary decreases by 1 each pass - the last k positions are already sorted after k passes.

2. Why is a temporary variable (temp) needed when swapping two elements?

When you write myList[i] = myList[i+1], the original value of myList[i] is overwritten and lost. Without temp, you can never assign it to myList[i+1]. The temp variable saves the first value so both elements can be exchanged safely.

3. A list of 7 elements is sorted using bubble sort. What is the maximum number of passes needed?

A list of n elements requires at most n-1 passes. For n=7, that is 6 passes. After 6 passes, the 6 largest elements are each in their final positions, which means the smallest element must also be in its correct position.

4. What does the early termination optimisation detect to stop the sort early?

If a full pass completes with no swaps, every adjacent pair was already in the correct order - meaning the entire list is sorted. Further passes would also make no swaps, so the algorithm can terminate immediately.

5. Bubble sort has worst-case complexity O(n2). Which input causes the worst case?

A reverse-sorted list (e.g. [5,4,3,2,1]) is the worst case - every adjacent pair is out of order, requiring a swap in every comparison. This maximises the total number of comparisons and swaps. Note: with early termination, the best case (already sorted) becomes just one pass.
Challenge question

A student claims that bubble sort with early termination is always better than standard bubble sort. Describe a specific scenario where early termination makes a significant difference and another where it makes no difference at all. Then state the best and worst case number of passes for each scenario on a list of 100 items.

Early termination makes a big difference:
If the list is already sorted (e.g. [1,2,3,...,100]). Without early termination: 99 passes regardless. With early termination: just 1 pass (confirms no swaps needed), then stops. This is the best case for optimised bubble sort: O(n) = 100 comparisons total instead of O(n2) = ~4950.

Early termination makes no difference:
If the list is in reverse sorted order (e.g. [100,99,98,...,1]). Every pass will make at least one swap, so the early termination flag never stays false. Both versions run the full n-1 = 99 passes.

Summary:
Best case with early termination: 1 pass (list already sorted)
Worst case with early termination: 99 passes (reverse sorted list)
Standard bubble sort: always 99 passes regardless of input order.
Printable Worksheets

Practice what you have learned

Three levelled worksheets. Download, print and complete offline.

Recall
Bubble Sort Fundamentals
Define key terms, explain the swap mechanism and state maximum passes for given list sizes.
Download
Apply
Pass-by-Pass Traces
Show the list after each pass for three different starting lists. Identify where early termination would apply.
Download
Exam Style
Exam-Style Questions
Write pseudocode with early termination, trace a sort, count comparisons and compare bubble sort to other algorithms.
Download
Lesson 6 - Algorithms
Bubble Sort
Starter activity
Give groups of 5-6 students a set of numbered cards (1-6, shuffled). Tell them they can only compare and swap adjacent cards. Time how many passes each group needs to sort their cards. Debrief: what was the maximum number of passes needed? Did any group need fewer passes because their list happened to be nearly sorted? Use this to introduce early termination naturally.
Lesson objectives
1
Explain how bubble sort works and why large elements "bubble up" to the end.
2
Show the state of the list after each pass of a bubble sort trace.
3
Write pseudocode for bubble sort including the temp variable swap mechanism.
4
Explain the early termination optimisation and state when it makes the biggest difference.
5
State the worst-case complexity of bubble sort and identify which input causes it.
Key vocabulary
Bubble sort
Repeatedly compare and swap adjacent pairs. Largest element bubbles to end each pass.
Temp variable
Temporary store for the first element during a swap. Without it, the value is lost.
Early termination
Stop if a pass completes with no swaps. Best case O(n) instead of O(n2).
Discussion questions
How many passes does bubble sort need for a list of 10 items in the worst case? How many comparisons does that involve in total?
Can you swap two variables without a temp? (Hint: arithmetic method using addition and subtraction.) When would this break?
Why might a teacher use bubble sort to introduce sorting algorithms even though it is rarely used in real software?
Exit tickets
Show the list [4, 2, 7, 1, 5] after pass 1 of bubble sort. [2 marks]
Why is a temp variable needed during a swap? [1 mark]
State the maximum number of passes for a list of 9 items. [1 mark]
Homework suggestion
Write pseudocode for a modified bubble sort that sorts a list into descending order (largest first). Trace it on [5, 2, 8, 1, 9] to show the first two passes. Explain what one change to the comparison operator achieves this.