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
| Property | Selection sort | Merge sort |
|---|---|---|
| Technique | Repeatedly pick the smallest remaining value | 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) | No | Yes |
| In place (no extra array) | Yes | No |
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).
| Input | Selection sort | Merge sort |
|---|---|---|
| Mixed | 29 | 34 |
| Already sorted | 21 | 31 |
| Reversed | 27 | 29 |
| Nearly sorted | 23 | 31 |
| Duplicates | 31 | 32 |
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
- 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 Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge 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