Skip to content
SimpleScope

Sorting

Selection sort vs Merge sort

Selection sort and Merge sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Merge sort: divide and conquer: split, sort each half, merge.

At a glance

PropertySelection sortMerge sort
TechniqueRepeatedly pick the smallest remaining valueDivide 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)
Compares values with each otherYesYes
Stable (equal values keep their order)NoYes
In place (no extra array)YesNo

When to choose each

Selection sort: when to choose it

Choose selection sort for very small arrays, for a hard cap on writes, or when there is no memory to spare. It sorts inside the array with O(1) extra space and at most n − 1 swaps; merge sort allocates a copy of every element it merges, O(n) extra space, and writes each one back out at every level of the recursion. On arrays of a handful of elements that fixed overhead is what decides the result, as the counts below show.

Merge sort: when to choose it

Choose merge sort as soon as the array is more than tiny. It is O(n log n) in the best, average and worst case, while selection sort is O(n²) in all three — there is no input on which selection sort scales. Merge sort is also stable, where selection sort is not, and it is the algorithm that extends to data larger than memory and to sorting linked lists without random access. The cost is the extra array and the recursion.

The same inputs, counted

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

InputSelection sortMerge sort
Mixed2934
Already sorted2131
Reversed2729
Nearly sorted2331
Duplicates3132

What the numbers show

At seven elements selection sort wins all five cases, every one of them narrowly: 29 operations against 34 on the mixed array, 21 against 31 on the already-sorted one, 27 against 29 on the reversed one, 23 against 31 on the nearly sorted one and 31 against 32 on the duplicates.

That is a statement about n, not about the algorithms. Merge sort writes 20 values on every input here — each element copied back once per level of recursion — before any comparison is counted, and 20 is already more than selection sort's entire operation total on three of the five arrays. Selection sort, meanwhile, pays a fixed 21 comparisons and then almost nothing: as few as 0 writes on the sorted array and 3 swaps on the reversed one.

The crossover arrives quickly. For seven elements n(n − 1)/2 = 21 and n log₂ n ≈ 19.7, so the two are the same size and constants decide. At n = 100 it is 4,950 against about 664; at n = 10,000, 50 million against about 133,000. Nothing in selection sort's favour here survives past a couple of dozen elements, which is exactly why hybrid sorts use a small-array sort below a threshold and merge or quick sort above it.

See them run

More comparisons