Skip to content

Bubble sort

ordering

Level 1 of 3

Also called Sinking sort.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

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.

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.

question 3

Which statement about bubble sort's comparisons is both true and tight?

the answer, and why

the answer is A

A row in order costs one walk of n−1n - 1 comparisons, which grows like nn, from above and below.

this tests best, worst and average, on the learn page

if you chose another

  • A row back to front costs n(n−1)2\frac{n(n - 1)}{2}, which grows like n2n^2.
  • True, since OO is only a ceiling, but not tight.
  • On average the comparisons are at least the swaps, and those grow like n2n^2.

question 4

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.

pen and paper

question 5about 15 minutes

Show that bubble sort makes exactly n(n−1)2\frac{n(n - 1)}{2} comparisons on a row of nn numbers back to front.

  1. Say how many comparisons walk kk makes, and why no walk on this row stops early.
  2. Add up the walks, and show that the total is n(n−1)2\frac{n(n - 1)}{2}.
  3. Check your formula on a row of 5 numbers by counting every comparison.
a hint

Write the sum of the walks, then the same sum backwards beneath it, and add each column.

a worked answer

Walk kk compares the n−kn - k pairs of the first n−k+1n - k + 1 numbers. Each walk leaves the rest back to front, so every walk swaps and none stops early. The sum and the same sum backwards make nn in each of the n−1n - 1 columns, so twice the sum is n(n−1)n(n - 1). For 5 numbers, 4 + 3 + 2 + 1 = 10, and 5 times 4 over 2 is 10.

Why does a row back to front cost n(n−1)2\frac{n(n - 1)}{2} comparisons, the most any row can?
  1. the move: count what walk k looks at

    the line:
    after k−1 walks, walk k looksat the first n−(k−1) numbers\begin{gathered} \text{after } k - 1 \text{ walks, walk } k \text{ looks} \\ \text{at the first } n - (k - 1) \text{ numbers} \end{gathered}

    why it is allowed: Each walk leaves the biggest number of its part at the end, so the part loses one number a walk. For walk 2 of 5 numbers, the first 4.

  2. the move: open the bracket

    the line:
    that is the firstn−k+1 numbers\begin{gathered} \text{that is the first} \\ n - k + 1 \text{ numbers} \end{gathered}

    why it is allowed: Taking away k−1k - 1 is taking away kk and adding 1. For walk 2 of 5, 5 less 2, plus 1, is 4.

  3. the move: count the pairs among them

    the line:
    so walk k compares n−k pairs\text{so walk } k \text{ compares } n - k \text{ pairs}

    why it is allowed: Neighbouring pairs are one fewer than the numbers. 4 numbers make 3 pairs.

  4. the move: count the walks

    the line:
    there are at most n−1 walks\text{there are at most } n - 1 \text{ walks}

    why it is allowed: Put n−1n - 1 for kk in line 2. For 5 numbers, walk 4 looks at 5 less 4, plus 1, so 2 numbers. After it one number is left, with no pair. The sort may stop sooner, after a walk that swaps nothing.

  5. the move: no row costs more

    the line:
    no row of n numberscosts more than(n−1)+⋯+1\begin{gathered} \text{no row of } n \text{ numbers} \\ \text{costs more than} \\ (n - 1) + \dots + 1 \end{gathered}

    why it is allowed: Any row runs walk 1, walk 2 and on, at most n−1n - 1 walks by line 4, and walk kk compares n−kn - k pairs by line 3. For 4 numbers, at most 3 + 2 + 1.

  6. the move: see why this row never stops early

    the line:
    on a row back to front,every walk swaps\begin{gathered} \text{on a row back to front,} \\ \text{every walk swaps} \end{gathered}
    the row5, 4, 3, 2, 1
    after walk 14, 3, 2, 1, 5
    after walk 23, 2, 1, 4, 5
    after walk 32, 1, 3, 4, 5
    after walk 41, 2, 3, 4, 5
    Each walk carries the biggest number left to the end. The rest stay back to front, one number shorter.

    why it is allowed: Back to front, every pair of neighbours is wrong. Walk 1 carries the biggest number to the end. Each swap passes it over one other number, so the rest keep their order: 5, 4, 3 becomes 4, 5, 3, the 4 still before the 3. They stay back to front, so every walk starts with a wrong pair and swaps, and all n−1n - 1 walks run.

  7. the move: add up the walks

    the line:
    C=(n−1)+⋯+1C = (n - 1) + \dots + 1

    why it is allowed: CC is the comparisons on a row back to front. By line 6 every walk runs, and by line 3 they make n−1n - 1, then one fewer, down to 1. For 5 numbers, 4 + 3 + 2 + 1.

  8. the move: so this row is one of the dearest

    the line:
    a row back to front isone of the dearest\begin{gathered} \text{a row back to front is} \\ \text{one of the dearest} \end{gathered}

    why it is allowed: By line 5 no row costs more, and by line 7 this row costs just that.

  9. the move: write the same sum backwards

    the line:
    C=1+⋯+(n−1)C = 1 + \dots + (n - 1)

    why it is allowed: The same numbers in the other order. For 5 numbers, 1 + 2 + 3 + 4.

  10. the move: add the two lines, column by column

    the line:
    2C=n+⋯+n2C = n + \dots + n
    For a row of 5, line 9's 1, 2, 3, 4 in yellow and line 7's 4, 3, 2, 1 in blue. Each column is 5 tall, so together they fill a rectangle 4 wide and 5 tall, twice the sum.

    why it is allowed: Lay line 9 under line 7. Column kk holds n−kn - k and kk, which make nn.

  11. the move: count the columns

    the line:
    2C=n(n−1)2C = n(n - 1)

    why it is allowed: One column for each walk, n−1n - 1 columns of nn. For 5 numbers, 4 columns of 5, which is 20.

  12. the move: halve both sides

    the line:
    C=n(n−1)2C = \frac{n(n - 1)}{2}

    why it is allowed: For 5 numbers, half of 20 is 10, the 4 + 3 + 2 + 1 of line 7.

  13. the move: multiply out the bracket

    the line:
    C=n2−n2C = \frac{n^2 - n}{2}

    why it is allowed: nn lots of nn make n2n^2, and nn lots of 1 make nn.

  14. the move: halve each part

    the line:
    C=n22−n2C = \frac{n^2}{2} - \frac{n}{2}

    why it is allowed: Half of a difference is the difference of the halves.

  15. the move: see what a long row costs

    the line:
    for a long row, C is close to n22\text{for a long row, } C \text{ is close to } \frac{n^2}{2}

    why it is allowed: Line 14 takes n2\frac{n}{2} from n22\frac{n^2}{2}. For 2,000 numbers, 1,000 from 2,000,000.

  16. the move: square ten times the numbers

    the line:
    (10n)2=100 n2(10n)^2 = 100\,n^2

    why it is allowed: 10n10n times 10n10n, and 10 times 10 is 100.

  17. the move: ten times the numbers

    the line:
    ten times the numberscost about 100 timesthe comparisons\begin{gathered} \text{ten times the numbers} \\ \text{cost about 100 times} \\ \text{the comparisons} \end{gathered}

    why it is allowed: Put 10n10n for nn in line 15, and line 16 makes its square 100 times bigger. 1,000 numbers cost 499,500 comparisons and 10,000 cost 49,995,000, about 100 times. On section 2's plot that is one step across and two steps up.

  18. the move: a ceiling

    the line:
    C≤n22C \le \frac{n^2}{2}

    why it is allowed: Line 14 takes something away from n22\frac{n^2}{2}.

  19. the move: read it as a bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: By line 18, CC stays under half of n2n^2, however long the row.

  20. the move: a fact for the floor

    the line:
    n2≤n24 for n≥2\frac{n}{2} \le \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: Line 14 takes n2\frac{n}{2} away. For a floor, CC must stay above a fixed share of n2n^2, so swap that n2\frac{n}{2} for n24\frac{n^2}{4}, at least as big. Any share under a half would do; a quarter leaves a quarter. Times 4, this line says twice nn is at most nn times nn, true once nn is 2 or more. For 10 numbers, 5 is at most 25.

  21. the move: a floor

    the line:
    C≥n22−n24 for n≥2C \ge \frac{n^2}{2} - \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: Swap line 14's n2\frac{n}{2} for line 20's n24\frac{n^2}{4}, at least as big; taking that away leaves no more. For 10 numbers, 45 is at least 50 less 25.

  22. the move: tidy the floor

    the line:
    C≥n24 for n≥2C \ge \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: Half less a quarter is a quarter.

  23. the move: read it as a bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)

    why it is allowed: By line 22, CC never drops below a quarter of n2n^2 from 2 numbers on. A floor need only hold for rows long enough; at 1 number CC is 0.

  24. the move: a ceiling and a floor together

    the line:
    C∈Θ(n2)C \in \Theta(n^2)

    why it is allowed: Lines 23 and 19 bound CC by the same growth, n2n^2.

try it with a small nn: at n=4n = 4, line 7 gives C=6C = 6, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • walk kk makes n−kn - k comparisons, the part losing one number a walk
  • no walk stops early, since each starts with a wrong pair
  • the walks added to n(n−1)2\frac{n(n - 1)}{2}
  • the count of 10 for 5 numbers

question 6about 12 minutes

A program keeps a list of numbers in order. Each day a new number is written at the end, so the list holds nn numbers, and bubble sort sorts it again.

  1. How many comparisons does bubble sort make when the new number is bigger than every other?
  2. How many when it is smaller than every other? Show your working.
  3. Would you keep bubble sort for this job, or use insertion sort, which section 6 of the learn page describes? Say why in two or three sentences.
a hint

Watch which way the new number has to travel, and how far one walk carries it.

a worked answer

A new biggest number is already in place. One walk compares the n−1n - 1 pairs, swaps nothing, and the sort stops. A new smallest number moves one place a walk, so every walk runs, as the ladder shows. For a list of 1,000: a new biggest number costs bubble sort 999 comparisons and insertion sort 999, one check for each number after the first. A new smallest costs bubble sort 499,500 and insertion sort 998 + 999 = 1,997, the 998 checks then a slide past all 999 others. Insertion sort costs no more in the first case and far less in the second, so it is the better choice.

What does the list cost when its new number is the smallest?
  1. the move: follow the new number

    the line:
    each walk moves the newnumber one place to the left\begin{gathered} \text{each walk moves the new} \\ \text{number one place to the left} \end{gathered}
    the list2, 4, 6, 8, 1
    after walk 12, 4, 6, 1, 8
    after walk 22, 4, 1, 6, 8
    after walk 32, 1, 4, 6, 8
    after walk 41, 2, 4, 6, 8
    The 1 moves one place left a walk, and every walk swaps.

    why it is allowed: The new number and the one before it are the only wrong pair of neighbours. That pair is the last a walk compares, so each walk swaps it and ends. The part then loses its last number.

  2. the move: see that no walk stops early

    the line:
    the new number needs n−1walks, and every one swaps\begin{gathered} \text{the new number needs } n - 1 \\ \text{walks, and every one swaps} \end{gathered}

    why it is allowed: It must travel n−1n - 1 places to the front, one place a walk.

  3. the move: add up the walks

    the line:
    C=(n−1)+⋯+1C = (n - 1) + \dots + 1

    why it is allowed: CC is this list's comparisons. Walk kk compares n−kn - k pairs, by the learn page's first ladder, line 3. For 5 numbers, 4 + 3 + 2 + 1.

  4. the move: the worst case's sum

    the line:
    C=n(n−1)2C = \frac{n(n - 1)}{2}

    why it is allowed: It is the sum the learn page adds up for a row back to front. For 5 numbers, 10, as dear as a row back to front.

try it with a small nn: at n=4n = 4, line 3 gives C=6C = 6, which you can work out by hand

this tests where you meet it, on the learn page

what a full answer includes
  • one walk of n−1n - 1 comparisons for a new biggest number
  • for a new smallest number, every walk swaps once, so none stops early
  • the total n(n−1)2\frac{n(n - 1)}{2}, with working
  • a choice defended by these costs