DOn average the comparisons are at least the swaps, and those grow like .
4question 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
AThe 6 reaches the end in walk 1, and walk 2 swaps nothing, so 9.
CThe 6 reaches its place in walk 1, and walk 2 swaps nothing, so 9.
DThe 5 and the 6 reach the end in two walks, and a third swaps nothing, so 12.
pen and paper
5question 5about 15 minutes
Show that bubble sort makes exactly comparisons on a row of numbers back to front.
Say how many comparisons walk makes, and why no walk on this row stops early.
Add up the walks, and show that the total is .
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 compares the pairs of the first 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 in each of the columns, so twice the sum is . For 5 numbers, 4 + 3 + 2 + 1 = 10, and 5 times 4 over 2 is 10.
Why does a row back to front cost comparisons, the most any row can?24 of 24 shown
1the move: count what walk k looks at
the line:
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.
2the move: open the bracket
the line:
why it is allowed: Taking away is taking away and adding 1. For walk 2 of 5, 5 less 2, plus 1, is 4.
guess it, then
3the move: count the pairs among them
the line:
why it is allowed: Neighbouring pairs are one fewer than the numbers. 4 numbers make 3 pairs.
guess it, then
4the move: count the walks
the line:
why it is allowed: Put for 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.
guess it, then
5the move: no row costs more
the line:
why it is allowed: Any row runs walk 1, walk 2 and on, at most walks by line 4, and walk compares pairs by line 3. For 4 numbers, at most 3 + 2 + 1.
guess it, then
6the move: see why this row never stops early
the line:
the row
5, 4, 3, 2, 1
after walk 1
4, 3, 2, 1, 5
after walk 2
3, 2, 1, 4, 5
after walk 3
2, 1, 3, 4, 5
after walk 4
1, 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 walks run.
guess it, then
7the move: add up the walks
the line:
why it is allowed: is the comparisons on a row back to front. By line 6 every walk runs, and by line 3 they make , then one fewer, down to 1. For 5 numbers, 4 + 3 + 2 + 1.
guess it, then
8the move: so this row is one of the dearest
the line:
why it is allowed: By line 5 no row costs more, and by line 7 this row costs just that.
guess it, then
9the move: write the same sum backwards
the line:
why it is allowed: The same numbers in the other order. For 5 numbers, 1 + 2 + 3 + 4.
guess it, then
10the move: add the two lines, column by column
the line:
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 holds and , which make .
guess it, then
11the move: count the columns
the line:
why it is allowed: One column for each walk, columns of . For 5 numbers, 4 columns of 5, which is 20.
guess it, then
12the move: halve both sides
the line:
why it is allowed: For 5 numbers, half of 20 is 10, the 4 + 3 + 2 + 1 of line 7.
guess it, then
13the move: multiply out the bracket
the line:
why it is allowed: lots of make , and lots of 1 make .
guess it, then
14the move: halve each part
the line:
why it is allowed: Half of a difference is the difference of the halves.
guess it, then
15the move: see what a long row costs
the line:
why it is allowed: Line 14 takes from . For 2,000 numbers, 1,000 from 2,000,000.
guess it, then
16the move: square ten times the numbers
the line:
why it is allowed: times , and 10 times 10 is 100.
guess it, then
17the move: ten times the numbers
the line:
why it is allowed: Put for 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.
guess it, then
18the move: a ceiling
the line:
why it is allowed: Line 14 takes something away from .
guess it, then
19the move: read it as a bound
the line:
why it is allowed: By line 18, stays under half of , however long the row.
guess it, then
20the move: a fact for the floor
the line:
why it is allowed: Line 14 takes away. For a floor, must stay above a fixed share of , so swap that for , at least as big. Any share under a half would do; a quarter leaves a quarter. Times 4, this line says twice is at most times , true once is 2 or more. For 10 numbers, 5 is at most 25.
guess it, then
21the move: a floor
the line:
why it is allowed: Swap line 14's for line 20's , at least as big; taking that away leaves no more. For 10 numbers, 45 is at least 50 less 25.
guess it, then
22the move: tidy the floor
the line:
why it is allowed: Half less a quarter is a quarter.
guess it, then
23the move: read it as a bound
the line:
why it is allowed: By line 22, never drops below a quarter of from 2 numbers on. A floor need only hold for rows long enough; at 1 number is 0.
guess it, then
24the move: a ceiling and a floor together
the line:
why it is allowed: Lines 23 and 19 bound by the same growth, .
guess it, then
try it with a small : at , line 7 gives , which you can work out by hand
walk makes comparisons, the part losing one number a walk
no walk stops early, since each starts with a wrong pair
the walks added to
the count of 10 for 5 numbers
6question 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 numbers, and bubble sort sorts it again.
How many comparisons does bubble sort make when the new number is bigger than every other?
How many when it is smaller than every other? Show your working.
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 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?4 of 4 shown
1the move: follow the new number
the line:
the list
2, 4, 6, 8, 1
after walk 1
2, 4, 6, 1, 8
after walk 2
2, 4, 1, 6, 8
after walk 3
2, 1, 4, 6, 8
after walk 4
1, 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.
2the move: see that no walk stops early
the line:
why it is allowed: It must travel places to the front, one place a walk.
guess it, then
3the move: add up the walks
the line:
why it is allowed: is this list's comparisons. Walk compares pairs, by the learn page's first ladder, line 3. For 5 numbers, 4 + 3 + 2 + 1.
guess it, then
4the move: the worst case's sum
the line:
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.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand