Skip to content
SimpleScope

Sorting

Insertion sort vs Merge sort

Insertion sort and Merge sort both put an array in order, in different ways. Insertion sort: insert each value into a sorted prefix. Merge sort: divide and conquer: split, sort each half, merge. In the worst case merge sort takes O(n log n) while insertion sort takes O(n²), so merge sort scales better on large inputs.

At a glance

PropertyInsertion sortMerge sort
TechniqueInsert each value into a sorted prefixDivide and conquer: split, sort each half, merge
Best case timeO(n)O(n log n)
Average case timeO(n²)O(n log n)
Worst case timeO(n²)O(n log n)
Extra spaceO(1)O(n)
Stable (equal values keep their order)YesYes
In place (no extra array)YesNo
Needs sorted inputNoNo

When to choose each

Choose insertion sort when

the array is small or nearly sorted.

Avoid it when

the array is large and in random order.

Choose merge sort when

you need a guaranteed O(n log n) time or a stable sort.

Avoid it when

memory is tight.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).

InputInsertion sortMerge sort
Mixed3034
Already sorted1231
Reversed4829
Nearly sorted1431
Duplicates3632

See them run

More comparisons