Quick Sort vs Cocktail Shaker Sort
Across our 3 test inputs, Cocktail Shaker Sort needed 101 comparisons in total versus 106 for Quick Sort. On average Quick Sort runs in O(n log n) time and Cocktail Shaker Sort in O(n²). Cocktail Shaker Sort is stable; Quick Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Quick Sort | Cocktail Shaker Sort |
|---|---|---|
| Random order | 22 | 39 |
| Nearly sorted | 39 | 17 |
| Reversed | 45 | 45 |
Complexity
| Quick Sort | Cocktail Shaker Sort | |
|---|---|---|
| Average time | O(n log n) | O(n²) |
| Worst time | O(n²) | O(n²) |
| Space | O(log n) | O(1) |
| Stable | No | Yes |
When to use Quick Sort
General-purpose sorting. Fast in practice due to excellent cache locality. Default algorithm used in many language libraries (often hybridised as Introsort).
When to use Cocktail Shaker Sort
Teaching, or small arrays where small values sit near the end (where plain Bubble Sort is slow).