Bubble Sort vs Cocktail Shaker Sort
Across our 3 test inputs, Cocktail Shaker Sort needed 101 comparisons in total versus 135 for Bubble Sort.
Head-to-head on identical inputs (comparisons)
| Input | Bubble Sort | Cocktail Shaker Sort |
|---|---|---|
| Random order | 45 | 39 |
| Nearly sorted | 45 | 17 |
| Reversed | 45 | 45 |
Complexity
| Bubble Sort | Cocktail Shaker Sort | |
|---|---|---|
| Average time | O(n²) | O(n²) |
| Worst time | O(n²) | O(n²) |
| Space | O(1) | O(1) |
| Stable | Yes | Yes |
When to use Bubble Sort
Educational purposes and very small arrays. Never use in production due to O(n²) worst-case.
When to use Cocktail Shaker Sort
Teaching, or small arrays where small values sit near the end (where plain Bubble Sort is slow).