Bubble Sort

Learn how the Bubble Sort algorithm organizes data by swapping adjacent elements.

Warm Up

Imagine that you want to sort these 7 cards into ascending order (Lowest to Highest). Click a card then click another to swap them.

Swaps: 0

Click two cards to swap their positions.

What is Bubble Sort?

The bubble sort algorithm works by repeatedly going through a list of items, comparing consecutive pairs of items and swapping the items if they are in the wrong order.

The algorithm is called bubble sort because when sorting a list from lowest to highest value, the highest values are moved up the list, which some people imagine as bubbles in a fizzy drink rising to the top.

  • Pass: Each time the algorithm goes through the list completely.
  • Comparison: Each time a pair of items are checked to see if they are in the correct order.

Visualising the Process

First Pass

The algorithm compares start to end. The highest value (10) bubbles to the top.
At the end of this pass, the highest value (10) will be green.

9
0
3
1
10
2
2
3
8
4
5
5
1
6
Pass1
Comparisons0
Swaps0

Using this method, if there are N items in the list, then a maximum of N-1 passes will be performed. Optimised versions of the algorithm stop early if no swaps are made in a pass.

Numerical Example: Olympic Medals

Let's sort the medal counts: [15, 25, 13, 29, 18].

1. Sorting Numbers

15
0
25
1
13
2
29
3
18
4
Pass1
Comparisons0
Swaps0

Try it yourself!

How many comparisons will bubble sort perform with the following list during the first pass?

12
10
9
6
6
5
5

List Length = 7

Sorting Words

Bubble sort can also sort words strictly alphabetically (A-Z).

2. Sorting Schools

Swindon Academy
0
Lawn Manor
1
Abbey Park
2
The Deanery
3
Great Western
4
Nova Hreod
5
Commonweal
6
Pass1
Comparisons0
Swaps0

Try it yourself!

Consider this list of Swindon schools: ["Dorcan", "Highworth", "St Joseph's", "Kingsdown"].

How many swaps and how many comparisons will be made during the first pass?

Algorithm Implementation

Below is the standard version of Bubble Sort. Use the trace table to step through the algorithm.

Loading trace data...
def bubble_sort(items):
    num_items = len(items)
    # Outer loop for number of passes
    for pass_num in range(num_items - 1):
        # Inner loop for comparisons
        for index in range(num_items - 1):
            if items[index] > items[index + 1]:
                # Swap elements
                temp = items[index]
                items[index] = items[index + 1]
                items[index + 1] = temp
    return items

# This basic version is easy to code but not very efficient because it keeps checking even if the list is already sorted.

Efficiency Scenarios

Bubble sort performance varies greatly depending on the initial order.

Best Case

Already sorted. 1 Pass. 0 Swaps.

1
0
2
1
3
2
4
3
5
4
Pass1
Comparisons0
Swaps0

Worst Case

Reverse order. Max Passes & Swaps.

5
0
4
1
3
2
2
3
1
4
Pass1
Comparisons0
Swaps0

Fill the Gaps

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Order the Steps

Arrange the steps of the Bubble Sort algorithm.

Available Steps:

Your sequence:

Click items above to build your sequence

Keywords Memory Game

A-Level Only: Efficiency of Bubble Sort

Let's analyse the Time and Space complexity of Bubble Sort using Big O notation. The graph below compares the operations needed for Best Case (Linear O(n)) and Worst/Average Case (Polynomial O(n²)), alongside the Space Complexity (Constant O(1)).

Did you know?

Click to flip

Bubble sort isn't always the slowest option! While it performs poorly on average, it actually beats many advanced algorithms when a list is already sorted (its best case scenario).

Big-O notation focuses on the worst-case performance, which is why Bubble Sort is often considered inefficient. However, another metric called Big-Omega (Ω) measures the best case, where Bubble Sort can actually have an advantage!

Fill the Gaps

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)

Available options:(Click option to fill active gap)