Cocktail Shaker Sort
Alternate forward and backward Bubble Sort passes so both large and small misplaced values move quickly.
Core idea
Alternate a left-to-right bubble pass with a right-to-left pass. The forward scan fixes a large value at the right boundary, while the backward scan fixes a small value at the left boundary.
Read the visualization
The active direction changes at each boundary. Adjacent pointers mark each comparison, swaps move stable bars, and the completed prefix and suffix close around the unsorted center.
Start a forward pass that moves the largest remaining value to the right.
Complexity and tradeoffs
Time: O(n^2). Space: O(1). Stable and in-place; bidirectional passes help some inputs but do not change the quadratic bound.
Where it fits
Cocktail Shaker Sort is mainly educational. It can improve over one-way Bubble Sort when small values begin near the right edge, but efficient general sorting still favors n log n methods.