Skip to content

Cocktail shaker sort

ordering

Level 1 of 3

Also called Shaker sort and Bidirectional bubble 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 cocktail shaker sort's own run on the numbers shown.

choose one

question 1

Cocktail shaker sort works on 2, 9, 8, 6, 7, 5. What is the row after its first two walks, one right and one back left?

298675
the answer, and why

the answer is A

After walk 1 the 9 stands at the end: 2, 8, 6, 7, 5, 9. After walk 2, going back left, the 2 stands at the front: 2, 5, 8, 6, 7, 9.

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 sorted. Two walks promise only that the biggest and the smallest reach their ends.

new numbersthe test page's numbers

question 2

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

541983
the answer, and why

the answer is D

Its walks compare 5 + 4 + 3 + 2 pairs, 14 in all. Walk 4 swaps nothing, so the sort stops there.

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.

new numbersthe test page's numbers

question 3

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

Cocktail shaker sort makes 7 swaps on 4, 5, 2, 1, 3, the most of the four.

if you chose another

  • Cocktail shaker sort makes 5 swaps on this row, fewer than 7.
  • Cocktail shaker sort makes 2 swaps on this row, fewer than 7.
  • Cocktail shaker sort makes 5 swaps on this row, fewer than 7.

new numbersthe test page's numbers