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
- Like bubble sort, but it goes in both directions.
- Forward pass: bubble the largest remaining value to the right end.
- Backward pass: bubble the smallest remaining value to the left end.
- Shrink the unsorted middle and repeat until no swaps happen.
Watch it run
Loading animation…