Cocktail shaker sort works on 3, 6, 2, 5, 1, 4. What is the row after its first two walks, one right and one back left?
the answer, and why
the answer is A
Walk 1 carries the 6 to the end, leaving 3, 2, 5, 1, 4, 6. Walk 2 goes left from the 4, and the 1 swaps past the 5, the 2 and the 3. The 5 lands beside the 4 only after walk 2 has passed places 4 and 5, so the 5 and the 4 stay the wrong way round.
3, 6, 2, 5, 1, 4, walk by walk.
if you chose another
BThat is the row after walk 1 only.
CThat is two walks to the right, as bubble sort makes them.
DThat is the row after walk 3, which sorts it.
2question 2
How many comparisons does cocktail shaker sort make, from start to finish, on 5, 1, 4, 3, 6, 2?
the answer, and why
the answer is A
The four walks compare 5, 4, 3 and 2 pairs. Walk 3 leaves the row in order, but only walk 4, swapping nothing, stops the sort. 5 + 4 + 3 + 2 = 14.
5, 1, 4, 3, 6, 2, walk by walk.
if you chose another
BThat is bubble sort's count on this row.
CThat counts the swaps, one for each wrong pair.
DThat stops after walk 3, when the row is first in order, but walk 4 still runs to find no swap.
3question 3
Which statement about cocktail shaker sort's comparisons on a shuffled row, on average, is both true and tight?
the answer, and why
the answer is D
The swaps average , and each follows a comparison, while no row costs more than . The floor and the ceiling share the growth .
AThat is a row already in order. On a shuffled row the swaps alone grow like .
BThe average is at least , times a quarter of . That quarter is 10 at 41 numbers and 100 at 401, past any fixed number. So no fixed multiple of stays above it.
CTrue but not tight. The average's floor, , is times a quarter of , at least once is at least 4, from 5 numbers on. The tight bound needs .
4question 4
Cocktail shaker sort works on each of these rows of 1 to 5. On which does it make the most swaps?
the answer, and why
the answer is A
Its swaps are the row's wrong pairs. The 5 stands before all 4 smaller numbers, and no other pair is wrong, so 4 swaps, all in walk 1.
5, 1, 2, 3, 4, swap by swap.
if you chose another
B3 and 1, 3 and 2, and 5 and 4 are wrong, so 3 swaps.
C2 and 1, and 4 and 3, are wrong, so 2 swaps.
D3 and 2, and 5 and 4, are wrong, so 2 swaps.
pen and paper
5question 5about 15 minutes
Show that, on a row of different numbers back to front, cocktail shaker sort makes comparisons, that no row makes more, and that this count is .
Count the pairs walk compares, and the most walks.
Say why no walk stops this row early, and add up the walks.
Find a ceiling and a floor of the same term, and name the tight bound.
a hint
Each walk leaves one more number at an end.
a worked answer
Walk compares pairs, and at most walks run. Back to front, each walk leaves the part between back to front, so every walk swaps and all run. The sum is , at most and at least from 2 numbers on. For 4 numbers, 3 + 2 + 1 makes 6.
Why does a row of different numbers back to front take comparisons, the most any row can?23 of 23 shown
1the move: count the numbers walk k looks at
the line:
why it is allowed: Each earlier walk swapped, or the sort would have stopped, and left one more number at an end. For walk 2 of 5 numbers, 5 less 1.
2the move: open the bracket
the line:
why it is allowed: Take away , then give 1 back. For walk 2 of 5 numbers, 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: With for , line 3 gives 1 pair, then one number is left. For 5 numbers, walk 4 looks at 2 numbers, 1 pair. The sort may stop sooner, after a walk that swaps nothing.
guess it, then
5the move: no row costs more
the line:
5, 4, 3, 2, 1, walk by walk.
why it is allowed: By lines 3 and 4, any row runs at most walks 1 to , walk comparing pairs. For 5 numbers, at most 4 + 3 + 2 + 1, as drawn.
guess it, then
6the move: see why this row never stops early
the line:
why it is allowed: With different numbers back to front, every pair of neighbours is wrong. Each walk carries the biggest or smallest still unplaced to its end, each swap passing it over one number. So the part between stays back to front, one shorter: in line 5's picture 4, 3, 2, 1 after walk 1, then 4, 3, 2. So every walk swaps, and all run.
guess it, then
7the move: add up the walks
the line:
why it is allowed: is the comparisons on this row. By lines 6 and 3 all walks run, comparing , then one fewer, down to 1. For 4 numbers, 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. It is one of the dearest, not the only one. 2, 3, 1 costs 3, as 3, 2, 1 does.
guess it, then
9the move: add them the other way round
the line:
why it is allowed: The same numbers in the other order.
guess it, then
10the move: use the page on adding up, its last number here being n - 1
the line:
where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make?
1the move: draw the sum as a staircase
the line:
why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
2the move: take two staircases
the line:
why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
3the move: fit them into a rectangle
the line:
Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.
why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
4the move: halve both sides
the line:
why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
every walk swaps on this row, with a reason, so all run
the sum n(n - 1)/2, with a reason
a ceiling and a floor of n squared, and the tight bound named
6question 6about 15 minutes
A row of different numbers was in order, smallest first, until its first and last numbers were traded. It now reads , then 2, 3, and on to , then 1.
How many comparisons does cocktail shaker sort make on it? Work it out walk by walk.
How many swaps does it make?
Counted on this site, bubble sort makes 4,950 comparisons on this row of 100 numbers. Compare, and say which number holds bubble sort back.
a hint
Follow the biggest number on the first walk and the 1 on the second.
a worked answer
Walk 1 carries the to the end and walk 2 the 1 to the front, each comparison a swap. Walk 3 swaps nothing. From 3 numbers on, that is comparisons and swaps, 294 and 197 for 100 numbers. Bubble sort moves the 1 one place a walk, so all 99 of its walks run.
What does the row with its ends traded cost?7 of 7 shown
1the move: walk 1 carries the biggest to the end
the line:
5, 2, 3, 4, 1, walk by walk.
why it is allowed: Take at least 3; line 7 says why. Walk 1 carries the past every number, each comparison a swap.
2the move: walk 2 carries the 1 to the front
the line:
why it is allowed: Walk 2 goes left from the 1, at place , over places 1 to , so pairs. Each has the 1 just right of a bigger number, so each swaps, and the row is in order.
guess it, then
3the move: walk 3 swaps nothing
the line:
why it is allowed: Places 2 to make pairs, all in order, so the sort stops. With 3 numbers walk 3 adds 0.
guess it, then
4the move: count the swaps
the line:
walk on 5, 2, 3, 4, 1
comparisons
swaps
1, right
4
4
2, left
3
3
3, right
2
0
in all
9
7
Walks 1 and 2 swap at every pair, and walk 3 swaps nothing.
why it is allowed: Each comparison of walks 1 and 2 swaps, and walk 3 swaps none. For 5 numbers, 4 + 3.
guess it, then
5the move: collect the swaps' terms
the line:
why it is allowed: Two s, less 1 and 2. For 5 numbers, 7, and for 100, 197.
guess it, then
6the move: add up the walks
the line:
why it is allowed: is the comparisons on this row, lines 1 to 3 added up. For 5 numbers, 4 + 3 + 2.
guess it, then
7the move: collect the terms
the line:
why it is allowed: Three s, less 1, 2 and 3. For 5 numbers, 9. At 2 numbers it gives 0, but 2, 1 takes 1 comparison.
guess it, then
try it with a small : at , line 6 gives , which you can work out by hand