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. In the worst case merge sort takes O(n log n) while selection sort takes O(n²), so merge sort scales better on large inputs.

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)
Stable (equal values keep their order)NoYes
In place (no extra array)YesNo
Needs sorted inputNoNo

When to choose each

Choose selection sort when

writing to memory is expensive, since it makes at most n - 1 swaps.

Avoid it when

the data is large, or you hope sorted input will be fast.

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).

InputSelection sortMerge sort
Mixed2934
Already sorted2131
Reversed2729
Nearly sorted2331
Duplicates3132

See them run

More comparisons