Merge Sort vs Shell Sort
Across our 3 test inputs, Merge Sort needed 59 comparisons in total versus 79 for Shell Sort. On average Merge Sort runs in O(n log n) time and Shell Sort in Gap-dependent. Merge Sort is stable; Shell Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Merge Sort | Shell Sort |
|---|---|---|
| Random order | 25 | 29 |
| Nearly sorted | 19 | 23 |
| Reversed | 15 | 27 |
Complexity
| Merge Sort | Shell Sort | |
|---|---|---|
| Average time | O(n log n) | Gap-dependent |
| Worst time | O(n log n) | O(n²) |
| Space | O(n) | O(1) |
| Stable | Yes | No |
When to use Merge Sort
When stable sorting is strictly required and O(n) extra space is acceptable. Highly predictable performance (always O(n log n)).
When to use Shell Sort
Medium-sized arrays and embedded systems where recursion or extra memory is undesirable.