sort.cny.shStart a race
Back to library
03 / 18Algorithm

Selection Sort

Find the smallest item remaining; put it at the front.

StableIn-placeUnstable
BestO(n²)
AverageO(n²)
WorstO(n²)
01How it works

Scan the unsorted region to find the minimum, swap it into the first unsorted slot, then repeat with a shrinking region. The number of swaps is exactly n−1 regardless of input, which is the lowest of any comparison sort.

02Best for

Situations where a write is far more expensive than a comparison — flash memory, EEPROM, slow external storage. When every swap costs real energy or erase cycles, the minimal-swap guarantee matters more than total comparisons.

03Avoid when

You care about wall-clock time. It does O(n²) comparisons even on already-sorted input, and it isn't stable — equal keys can get reordered.