Skip to main content
SingaporeComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.87 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

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 [4,1,3,2][4, 1, 3, 2] 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, 44 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 nn items needs at most nβˆ’1n - 1 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 [5,2,8,1][5, 2, 8, 1]. 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 [2,5,1,8][2, 5, 1, 8]. The largest value, 88, 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 [2,5,1,8][2, 5, 1, 8], and 88 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 nn 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 nβˆ’1n - 1 passes the list must be sorted.)

(c) For nn items, at most nβˆ’1n - 1 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 nβˆ’1n-1 passes), and the maximum of nβˆ’1n - 1 passes.

Related dot points