Insertion Sort vs Shell Sort
Across our 3 test inputs, Shell Sort needed 79 comparisons in total versus 85 for Insertion Sort. On average Insertion Sort runs in O(n²) time and Shell Sort in Gap-dependent. Insertion Sort is stable; Shell Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Insertion Sort | Shell Sort |
|---|---|---|
| Random order | 30 | 29 |
| Nearly sorted | 10 | 23 |
| Reversed | 45 | 27 |
Complexity
| Insertion 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 Insertion Sort
Small arrays or nearly-sorted arrays. O(n) best case for already-sorted input. Used as a subroutine in Timsort.
When to use Shell Sort
Medium-sized arrays and embedded systems where recursion or extra memory is undesirable.