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.
At a glance
| Property | Insertion sort | Merge sort |
|---|---|---|
| Technique | Insert each value into a sorted prefix | Divide and conquer: split, sort each half, merge |
| Best case time | O(n) | O(n log n) |
| Average case time | O(n²) | O(n log n) |
| Worst case time | O(n²) | O(n log n) |
| Extra space | O(1) | O(n) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | Yes | No |
When to choose each
Insertion sort: when to choose it
Choose insertion sort for small or nearly sorted arrays, and when you cannot allocate. It is O(1) extra space against merge sort's O(n), it is adaptive — its comparison count falls towards n as the input gets closer to sorted — and it is online, so it can absorb elements as they arrive. Both sorts are stable, so that is not the deciding factor; the decision is really between adaptivity with a quadratic ceiling and a flat guarantee that costs memory.
Merge sort: when to choose it
Choose merge sort when the array is large or its order is unknown. O(n log n) in the best, average and worst case means no input can make it slow, where insertion sort degrades to O(n²) on anything badly out of order. It is also the practical choice for data too big for memory and for linked structures without random access, since merging only ever reads forwards. In exchange it allocates a copy of the data and writes every element back on every level of the recursion.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).
| Input | Insertion sort | Merge sort |
|---|---|---|
| Mixed | 30 | 34 |
| Already sorted | 12 | 31 |
| Reversed | 48 | 29 |
| Nearly sorted | 14 | 31 |
| Duplicates | 36 | 32 |
What the numbers show
Where the input has order to exploit, insertion sort wins clearly: 12 operations against 31 on the already-sorted array and 14 against 31 on the nearly sorted one, roughly half the cost in both cases. Merge sort has to split and merge regardless, and its 20 writes per run are paid in full even when every value is already in place. On the mixed array insertion sort still edges ahead, 30 against 34.
Where the input is genuinely disordered, merge sort wins: 29 against 48 on the reversed array and 32 against 36 on the duplicates. The reversed array shows both mechanisms at once. Insertion sort hits its worst case — 21 comparisons and 27 writes, because every new key must be shifted past the entire prefix — while merge sort hits its cheapest input of the five at 9 comparisons, since each merge exhausts one side early.
Merge sort's whole range is 29 to 34, a spread of five operations; insertion sort's is 12 to 48, a spread of 36. That contrast is the comparison. At n = 7, n²/4 (insertion sort's average) and n log₂ n are both about 20, so the inputs decide; by n = 1,000 it is about 250,000 against about 10,000 and the guarantee wins outright. This is exactly why production sorts are hybrids: merge or quick sort for the recursion, insertion sort for the small runs at the bottom.
See them run
More comparisons
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort