How does bubble sort put a list in order one pass at a time?
Describe and trace the bubble sort, explaining how repeated passes of comparisons and swaps sort a list
A focused answer to the O-Level Computing point on sorting. How bubble sort compares neighbouring items and swaps them, how each pass moves the largest value to the end, and tracing the algorithm by hand.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to describe and trace the bubble sort, explaining how repeated passes of comparisons and swaps put a list into order. The central idea is that by comparing neighbouring items and swapping any that are out of order, each pass moves the largest remaining value to the end, so after enough passes the whole list is sorted.
The answer
What bubble sort does
A bubble sort sorts a list by repeatedly stepping through it, comparing each pair of neighbouring items and swapping them if they are in the wrong order. For an ascending sort, it swaps whenever the left item is larger than the right.
FOR each pass
FOR each neighbouring pair from the left
IF left > right THEN
swap them
ENDIF
NEXT
NEXT
Why it is called "bubble"
In each pass, the largest unsorted value keeps being swapped rightwards until it reaches the end of the unsorted part. It is as if the biggest value "bubbles up" to the top (the end) of the list.
A worked pass
Take and sort ascending. One pass:
[4, 1, 3, 2] 4 > 1, swap -> [1, 4, 3, 2]
[1, 4, 3, 2] 4 > 3, swap -> [1, 3, 4, 2]
[1, 3, 4, 2] 4 > 2, swap -> [1, 3, 2, 4]
After the pass, is in its final place at the end. The next pass works on the rest.
When is the list sorted?
The list is sorted when a complete pass makes no swaps, because that means every neighbouring pair is already in order. A bubble sort of items needs at most passes, since each pass fixes the position of at least one more value.
How a swap works
Swapping two values needs a temporary store so neither value is lost:
temp = a
a = b
b = temp
Examples in context
Example 1. Ordering exam scores. A short list of class scores can be bubble-sorted into rank order by hand: compare each pair, swap the out-of-order ones, and repeat. After each pass the next-highest score settles into place, which is easy to follow on paper.
Example 2. Tidying a small playlist. Putting a few songs in order of length is a bubble sort in miniature: compare neighbours, swap the longer-first pairs, and keep passing until a pass needs no swaps. For a handful of items it is simple and reliable, though slow for very long lists.
Try this
Q1. State what a bubble sort compares on each step. [2 marks]
- Cue. Each pair of neighbouring (adjacent) items in the list.
Q2. After the first ascending pass of a bubble sort, which value is guaranteed to be in its final position? [2 marks]
- Cue. The largest value, which has bubbled to the end of the list.
Q3. Explain how you know a bubble sort has finished. [2 marks]
- Cue. When a complete pass makes no swaps, every neighbouring pair is in order, so the list is sorted.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original5 marksTrace one complete pass of a bubble sort (ascending) on the list . Show the list after each comparison and swap, and state the value that is now in its final position.Show worked answer β
A pass compares each neighbouring pair from the left and swaps if the left is larger.
[5, 2, 8, 1] compare 5 and 2: 5 > 2, swap -> [2, 5, 8, 1]
[2, 5, 8, 1] compare 5 and 8: 5 < 8, no swap -> [2, 5, 8, 1]
[2, 5, 8, 1] compare 8 and 1: 8 > 1, swap -> [2, 5, 1, 8]
After one pass the list is . The largest value, , has bubbled to the end, so it is now in its final position.
Markers reward each neighbouring comparison, the correct swaps, the list after the pass , and being in its final place.
Original5 marks(a) Explain what a single pass of a bubble sort does. (b) State how you know the whole list is sorted. (c) For a list of items, state the maximum number of passes that may be needed.Show worked answer β
(a) A single pass goes through the list from the start, comparing each pair of neighbouring items and swapping them if they are in the wrong order. This moves the largest remaining value to the end of the unsorted part (it "bubbles up").
(b) The list is sorted when a complete pass makes no swaps, because that means every neighbouring pair is already in order. (Equivalently, after passes the list must be sorted.)
(c) For items, at most passes are needed, because each pass places at least one more value in its final position.
Markers reward a pass comparing and swapping neighbours, the no-swaps test (or passes), and the maximum of passes.
Related dot points
- Define an algorithm and represent its sequence, selection and iteration using standard flowchart symbols
A focused answer to the O-Level Computing point on algorithms and flowcharts. What an algorithm is, the standard flowchart symbols, and how to show sequence, selection and iteration in a flowchart.
- Write algorithms in pseudocode using input, output, assignment, selection (IF) and iteration (WHILE, FOR)
A focused answer to the O-Level Computing point on pseudocode. Writing algorithms with input, output, assignment, IF selection, and WHILE and FOR loops, in clear language-independent steps.
- Describe and trace linear search and binary search, and explain why binary search needs a sorted list
A focused answer to the O-Level Computing point on searching. How linear search checks each item in turn, how binary search halves a sorted list each step, and why binary search is faster but needs sorted data.
- Use a trace table to follow variable values through an algorithm, and choose normal, boundary and invalid test data
A focused answer to the O-Level Computing point on tracing and testing. Building a trace table to track variables through an algorithm, and choosing normal, boundary and invalid test data to find errors.
- Use Python for loops with range and while loops with a condition, including accumulators and avoiding infinite loops
A focused answer to the O-Level Computing point on Python loops. Using for with range for a known count, while with a condition for an unknown count, building accumulators, and avoiding infinite loops.