Insertion Sort vs Merge Sort

Across our 3 test inputs, Merge Sort needed 59 comparisons in total versus 85 for Insertion Sort. On average Insertion Sort runs in O(n²) time and Merge Sort in O(n log n).

Head-to-head on identical inputs (comparisons)

InputInsertion SortMerge Sort
Random order3025
Nearly sorted1019
Reversed4515

Complexity

Insertion SortMerge Sort
Average timeO(n²)O(n log n)
Worst timeO(n²)O(n log n)
SpaceO(1)O(n)
StableYesYes

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 Merge Sort

When stable sorting is strictly required and O(n) extra space is acceptable. Highly predictable performance (always O(n log n)).