Skip to content
StackDecode
SortingAnimated

Cocktail Shaker Sort

Bubble sort that alternates direction each pass.

Time

O(n²)

Extra space

O(1)

Use it for

Teaching; data with small items at the end.

How it works

  1. Like bubble sort, but it goes in both directions.
  2. Forward pass: bubble the largest remaining value to the right end.
  3. Backward pass: bubble the smallest remaining value to the left end.
  4. Shrink the unsorted middle and repeat until no swaps happen.

Watch it run

Loading animation…