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, 3, 8, 1, 2. What is the row when that walk ends?

63812
the answer, and why

the answer is C

The walk compares 4 pairs and swaps 3 times, carrying the 8 to the end: 3, 6, 1, 2, 8.

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, 8 and 2, which the walk also swaps.

new numbersthe test page's numbers

question 2

How many comparisons does bubble sort make on 8, 3, 1, 9, 7, from start to finish?

83197
the answer, and why

the answer is B

Walk by walk it compares 4 + 3 + 2 pairs, 9 in all. Walk 3 swaps nothing, so the sort stops there.

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 4 walks. Here walk 3 swaps nothing, so the sort stops there.
  • That counts the swaps, not the comparisons.

new numbersthe test page's 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 D

Bubble sort makes 14 comparisons on 2, 3, 4, 1, 5, 6, the most of the four.

if you chose another

  • Bubble sort makes 12 comparisons on this row, fewer than 14.
  • Bubble sort makes 9 comparisons on this row, fewer than 14.
  • Bubble sort makes 9 comparisons on this row, fewer than 14.

new numbersthe test page's numbers