# Cocktail Sort visualised: bubble sort that alternates direction

> Interactive Cocktail Sort (Shaker Sort): Bubble Sort in both directions. Tackles the turtle problem, worst case O(n^2), stable.

Address: https://marckimmel.de/en/informatik/sortieralgorithmen/cocktail-sort
Deutsche Fassung: https://marckimmel.de/informatik/sortieralgorithmen/cocktail-sort.md

Bubble Sort that alternates direction each pass and sorts both ends at once.

Cocktail Sort (also known as Shaker Sort or bidirectional Bubble Sort) is a variant of Bubble Sort. Unlike classical Bubble Sort which always traverses left to right, Cocktail Sort alternates direction: a forward pass pushes the largest element to the right end, then a backward pass pulls the smallest element to the left. This handles the so-called turtles, small elements stuck at the end of the array that only advance one position per Bubble pass, much faster. Worst case stays quadratic O(n²) as for Bubble Sort, best case on already sorted data is linear O(n). Memory is constant O(1) and the algorithm is stable.

Rarely used in practice because Insertion Sort offers similar benefits on nearly sorted data and runs faster. Cocktail Sort is mostly a teaching example showing how a small tweak to Bubble Sort dramatically improves performance on certain inputs.

## Related

- [Sorting Algorithms](https://marckimmel.de/en/informatik/sortieralgorithmen.md)
