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)

InputMerge SortShell Sort
Random order2529
Nearly sorted1923
Reversed1527

Complexity

Merge SortShell Sort
Average timeO(n log n)Gap-dependent
Worst timeO(n log n)O(n²)
SpaceO(n)O(1)
StableYesNo

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.