Skip to content

Cocktail shaker sort

ordering

Level 1 of 3

Also called Shaker sort and Bidirectional bubble 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

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?

362514
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.

362514start325146walk 15 pairs compared132546walk 24 pairs compared123456walk 33 pairs compared123456walk 42 pairs, no swapone walkin place for good
3, 6, 2, 5, 1, 4, walk by walk.

if you chose another

  • That is the row after walk 1 only.
  • That is two walks to the right, as bubble sort makes them.
  • That is the row after walk 3, which sorts it.

question 2

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

514362
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.

514362start143526walk 15 pairs compared124356walk 24 pairs compared123456walk 33 pairs compared123456walk 42 pairs, no swapone walkin place for good
5, 1, 4, 3, 6, 2, walk by walk.

if you chose another

  • That is bubble sort's count on this row.
  • That counts the swaps, one for each wrong pair.
  • That stops after walk 3, when the row is first in order, but walk 4 still runs to find no swap.

question 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 n(n−1)4\frac{n(n - 1)}{4}, and each follows a comparison, while no row costs more than n(n−1)2\frac{n(n - 1)}{2}. The floor and the ceiling share the growth n2n^2.

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

if you chose another

  • That is a row already in order. On a shuffled row the swaps alone grow like n2n^2.
  • The average is at least n(n−1)4\frac{n(n - 1)}{4}, nn times a quarter of n−1n - 1. That quarter is 10 at 41 numbers and 100 at 401, past any fixed number. So no fixed multiple of nn stays above it.
  • True but not tight. The average's floor, n(n−1)4\frac{n(n - 1)}{4}, is nn times a quarter of n−1n - 1, at least nn once n−1n - 1 is at least 4, from 5 numbers on. The tight bound needs n2n^2.

question 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.

51234start4 wrong pairs15234swap 13 wrong pairs12534swap 22 wrong pairs12354swap 31 wrong pair12345swap 4no wrong pairsa wrong pair, bigger before smallerjust swappedthe pair it mended
5, 1, 2, 3, 4, swap by swap.

if you chose another

  • 3 and 1, 3 and 2, and 5 and 4 are wrong, so 3 swaps.
  • 2 and 1, and 4 and 3, are wrong, so 2 swaps.
  • 3 and 2, and 5 and 4, are wrong, so 2 swaps.

pen and paper

question 5about 15 minutes

Show that, on a row of nn different numbers back to front, cocktail shaker sort makes n(n−1)2\frac{n(n - 1)}{2} comparisons, that no row makes more, and that this count is Θ(n2)\Theta(n^2).

  1. Count the pairs walk kk compares, and the most walks.
  2. Say why no walk stops this row early, and add up the walks.
  3. 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 kk compares n−kn - k pairs, and at most n−1n - 1 walks run. Back to front, each walk leaves the part between back to front, so every walk swaps and all run. The sum is n(n−1)2\frac{n(n - 1)}{2}, at most n22\frac{n^2}{2} and at least n24\frac{n^2}{4} from 2 numbers on. For 4 numbers, 3 + 2 + 1 makes 6.

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

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

    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.

  2. the move: open the bracket

    the line:
    walk k looks atn−k+1 numbers\begin{gathered} \text{walk } k \text{ looks at} \\ n - k + 1 \text{ numbers} \end{gathered}

    why it is allowed: Take away kk, then give 1 back. For walk 2 of 5 numbers, 5 less 2, plus 1, is 4.

  3. the move: count the pairs among them

    the line:
    so walk k comparesn−k neighbouring pairs\begin{gathered} \text{so walk } k \text{ compares} \\ n - k \text{ neighbouring pairs} \end{gathered}

    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: With n−1n - 1 for kk, 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.

  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}
    54321start43215walk 14 pairs compared14325walk 23 pairs compared13245walk 32 pairs compared12345walk 41 pair comparedone walkin place for good
    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 n−1n - 1, walk kk comparing n−kn - k pairs. For 5 numbers, at most 4 + 3 + 2 + 1, as drawn.

  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}

    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 n−1n - 1 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 this row. By lines 6 and 3 all walks run, comparing n−1n - 1, then one fewer, down to 1. For 4 numbers, 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. It is one of the dearest, not the only one. 2, 3, 1 costs 3, as 3, 2, 1 does.

  9. the move: add them the other way round

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

    why it is allowed: The same numbers in the other order.

  10. the move: use the page on adding up, its last number here being n - 1

    the line:
    C=(n−1)((n−1)+1)2C = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The box above shows 1 + 2 + ... + 5 is 5 times 6, halved, and so for any last number. For 4 numbers, 3 times 4, halved, is 6.

  11. the move: tidy the bracket

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

    why it is allowed: One more than n−1n - 1 is nn.

  12. the move: write the product the other way round

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

    why it is allowed: The same product.

  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, less nn lots of 1. For 4 numbers, 4 times 3 is 16 less 4.

  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. For 4 numbers, 12 halved is 8 less 2.

  15. the move: a ceiling

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

    why it is allowed: Line 14 takes n2\frac{n}{2}, above zero, from n22\frac{n^2}{2}. For 4 numbers, 6 is at most 8.

  16. the move: read it as a bound

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

    why it is allowed: By line 15, CC stays under half of n2n^2.

  17. the move: a fact for the floor

    the line:
    1≤n2 for n≥21 \le \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: Now a floor, for line 20. Half of nn is at least 1 from 2 numbers on. At 2 numbers, 1 is at most 1.

  18. the move: multiply both sides by n

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

    why it is allowed: Line 17 times nn, above zero, so the order holds. For 10 numbers, 10 is at most 50.

  19. the move: halve both sides

    the line:
    n2≤n24 for n≥2\frac{n}{2} \le \frac{n^2}{4} \text{ for } n \ge 2
    nnn2\frac{n}{2}n24\frac{n^2}{4}
    211
    424
    10525
    100502,500
    Equal at 2 numbers, then the quarter of n squared pulls ahead.

    why it is allowed: Line 18 halved. For 10 numbers, 5 is at most 25.

  20. 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
    nnthe first quarterthe second, less n2\frac{n}{2}CC
    2101
    4426
    10252045
    1002,5002,4504,950
    Half of n squared as two quarters, the second less half of n. At 10 numbers, 25 plus 20 is 45.

    why it is allowed: Line 14 takes away n2\frac{n}{2}, at most line 19's n24\frac{n^2}{4}, and taking away more leaves less. For 10 numbers, 45 is at least 50 less 25.

  21. 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.

  22. the move: read it as a bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)
    10210410610810101012101410110210310410510610710110210310410510610710810910101011101210131014101102103104105106107
    • C=n(n−1)2C = \frac{n(n - 1)}{2}
    • n22\frac{n^2}{2}
    • n24\frac{n^2}{4}
    CC runs between the other two, all three about two steps up for each step across.

    why it is allowed: By line 21, from 2 numbers on. At 1 number CC is 0, but a floor need only hold for rows long enough.

  23. the move: a ceiling and a floor together

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

    why it is allowed: Lines 22 and 16 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 k compares n - k pairs, at most n - 1 walks
  • 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

question 6about 15 minutes

A row of nn different numbers was in order, smallest first, until its first and last numbers were traded. It now reads nn, then 2, 3, and on to n−1n - 1, then 1.

  1. How many comparisons does cocktail shaker sort make on it? Work it out walk by walk.
  2. How many swaps does it make?
  3. 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 nn 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 3n−63n - 6 comparisons and 2n−32n - 3 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?
  1. the move: walk 1 carries the biggest to the end

    the line:
    walk 1 compares n−1 pairs,carrying n to the end\begin{gathered} \text{walk 1 compares } n - 1 \text{ pairs,} \\ \text{carrying } n \text{ to the end} \end{gathered}
    52341start23415walk 14 pairs compared12345walk 23 pairs compared12345walk 32 pairs, no swapone walkin place for good
    5, 2, 3, 4, 1, walk by walk.

    why it is allowed: Take nn at least 3; line 7 says why. Walk 1 carries the nn past every number, each comparison a swap.

  2. the move: walk 2 carries the 1 to the front

    the line:
    walk 2 compares n−2 pairs,carrying the 1 to the front\begin{gathered} \text{walk 2 compares } n - 2 \text{ pairs,} \\ \text{carrying the 1 to the front} \end{gathered}

    why it is allowed: Walk 2 goes left from the 1, at place n−1n - 1, over places 1 to n−1n - 1, so n−2n - 2 pairs. Each has the 1 just right of a bigger number, so each swaps, and the row is in order.

  3. the move: walk 3 swaps nothing

    the line:
    walk 3 compares n−3 pairsand swaps nothing\begin{gathered} \text{walk 3 compares } n - 3 \text{ pairs} \\ \text{and swaps nothing} \end{gathered}

    why it is allowed: Places 2 to n−1n - 1 make n−3n - 3 pairs, all in order, so the sort stops. With 3 numbers walk 3 adds 0.

  4. the move: count the swaps

    the line:
    the swaps are(n−1)+(n−2)\begin{gathered} \text{the swaps are} \\ (n - 1) + (n - 2) \end{gathered}
    walk on 5, 2, 3, 4, 1comparisonsswaps
    1, right44
    2, left33
    3, right20
    in all97
    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.

  5. the move: collect the swaps' terms

    the line:
    (n−1)+(n−2)=2n−3(n - 1) + (n - 2) = 2n - 3

    why it is allowed: Two nns, less 1 and 2. For 5 numbers, 7, and for 100, 197.

  6. the move: add up the walks

    the line:
    T=(n−1)+(n−2)+(n−3)for n≥3\begin{gathered} T = (n - 1) + (n - 2) + (n - 3) \\ \text{for } n \ge 3 \end{gathered}

    why it is allowed: TT is the comparisons on this row, lines 1 to 3 added up. For 5 numbers, 4 + 3 + 2.

  7. the move: collect the terms

    the line:
    T=3n−6 for n≥3T = 3n - 6 \text{ for } n \ge 3

    why it is allowed: Three nns, less 1, 2 and 3. For 5 numbers, 9. At 2 numbers it gives 0, but 2, 1 takes 1 comparison.

try it with a small nn: at n=5n = 5, line 6 gives T=9T = 9, which you can work out by hand

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

what a full answer includes
  • walk 1, n - 1 comparisons, each a swap
  • walk 2, n - 2 comparisons, each a swap
  • walk 3 swaps nothing, so 3n - 6 comparisons
  • 2n - 3 swaps, 197 for 100 numbers
  • 294 against 4,950, the 1 moving a place a walk named as the reason