Selection Sort vs Cocktail Shaker Sort
Across our 3 test inputs, Cocktail Shaker Sort needed 101 comparisons in total versus 135 for Selection Sort. Cocktail Shaker Sort is stable; Selection Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Selection Sort | Cocktail Shaker Sort |
|---|---|---|
| Random order | 45 | 39 |
| Nearly sorted | 45 | 17 |
| Reversed | 45 | 45 |
Complexity
| Selection Sort | Cocktail Shaker Sort | |
|---|---|---|
| Average time | O(n²) | O(n²) |
| Worst time | O(n²) | O(n²) |
| Space | O(1) | O(1) |
| Stable | No | Yes |
When to use Selection Sort
When memory writes are expensive (minimizes swaps). Otherwise, insertion sort is generally preferred.
When to use Cocktail Shaker Sort
Teaching, or small arrays where small values sit near the end (where plain Bubble Sort is slow).