Cocktail Shaker Sort vs Shell Sort
Across our 3 test inputs, Shell Sort needed 79 comparisons in total versus 101 for Cocktail Shaker Sort. On average Cocktail Shaker Sort runs in O(n²) time and Shell Sort in Gap-dependent. Cocktail Shaker Sort is stable; Shell Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Cocktail Shaker Sort | Shell Sort |
|---|---|---|
| Random order | 39 | 29 |
| Nearly sorted | 17 | 23 |
| Reversed | 45 | 27 |
Complexity
| Cocktail Shaker Sort | Shell Sort | |
|---|---|---|
| Average time | O(n²) | Gap-dependent |
| Worst time | O(n²) | O(n²) |
| Space | O(1) | O(1) |
| Stable | Yes | No |
When to use Cocktail Shaker Sort
Teaching, or small arrays where small values sit near the end (where plain Bubble Sort is slow).
When to use Shell Sort
Medium-sized arrays and embedded systems where recursion or extra memory is undesirable.