Skip to content

Bubble sort

ordering

Level 1 of 3

Also called Sinking sort.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from bubble sort's own run on the numbers shown.

choose one

question 1

Bubble sort makes its first walk along 6, 2, 9, 4, 7. What is the row when that walk ends?

62947
the answer, and why

the answer is A

The 6 and the 2 swap, the 6 and the 9 stay, then the 9 swaps with the 4 and with the 7, ending last.

if you chose another

  • That is the row sorted. One walk promises only that the biggest number reaches the end.
  • That stops after the first swap. A walk goes on to the end of its part.
  • That misses the last pair, 9 and 7, which the walk also swaps.

new numbers

question 2

How many comparisons does bubble sort make on 3, 1, 2, 4, 5, from start to finish?

31245
the answer, and why

the answer is A

Walk 1 compares 4 pairs and swaps twice, leaving 1, 2, 3, 4, 5. It swapped, so walk 2 compares 3 pairs, swaps nothing, and the sort stops. 4 + 3 = 7.

if you chose another

  • That is walk 1 alone. It swapped, so another walk runs.
  • That is the most 5 numbers can cost, with all four walks. Here walk 2 swaps nothing and the sort stops.
  • That counts the swaps, not the comparisons.

new numbers

question 3

Each row holds 1 to 6 with little out of place. On which does bubble sort make the most comparisons?

the answer, and why

the answer is B

A small number near the end moves left one place a walk. The 1 needs five walks, each with a swap, so 5 + 4 + 3 + 2 + 1 = 15, the most 6 numbers can cost.

if you chose another

  • The 6 reaches the end in walk 1, and walk 2 swaps nothing, so 9.
  • The 6 reaches its place in walk 1, and walk 2 swaps nothing, so 9.
  • The 5 and the 6 reach the end in two walks, and a third swaps nothing, so 12.

new numbers