Selection Sort
- Ω( n2 )
- Θ( n2 )
- O( n2 )
- space O( 1 )
Selection sort grows a sorted prefix one slot at a time: scan everything to the right of the boundary, find the smallest value, and move it into the next free position. The scan is the costly part and never gets cheaper than n(n-1)/2 comparisons, but the writing is frugal, because every pass ends in at most one swap. That lopsided split between reading and writing is the reason the algorithm is still taught and occasionally used.
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How selection sort works
Take 5, 2, 6, 1, 4, 3. The first pass reads the five values to the right of index 0, finds the 1 at index 3, and swaps it with the 5. The array becomes [1, 2, 6, 5, 4, 3]. Look at what happened to the 5: it travelled from the front to index 3 in a single move, past two other values. That long jump is cheap, and it is also the reason the sort cannot promise to keep equal values in their original order.
The second pass spends four comparisons confirming that the 2 is already the smallest of what remains, so it swaps nothing and the table has no row for it. The third pass scans 6, 5, 4, 3, picks the 3 at index 5 and swaps it into index 2, giving [1, 2, 3, 5, 4, 6]. The 6 reached its final cell as a side effect of that swap. The fourth pass scans 5, 4, 6, picks the 4 at index 4, and swaps it with the 5 at index 3 to give [1, 2, 3, 4, 5, 6].
That is three swaps for six elements. A last pass still compares 5 with 6 and finds nothing to do, bringing the total to 15 comparisons, which is 5 + 4 + 3 + 2 + 1. The same 15 would have been spent if the input had arrived sorted; only the swap count would have dropped to zero. The algorithm cannot tell that it has finished early, so it keeps scanning.
| Step | Change | Array after |
|---|---|---|
| 1 | Swap indexes 0 and 3 | 1 2 6 5 4 3 |
| 2 | Swap indexes 2 and 5 | 1 2 3 5 4 6 |
| 3 | Swap indexes 3 and 4 | 1 2 3 4 5 6 |
All 3 changes are shown.
Pseudocode
for i = 0 to n - 2
min = i
for j = i + 1 to n - 1
if a[j] < a[min]
min = j
if min != i
swap a[i], a[min]
Why these complexities
The inner loop always runs from i + 1 to the end of the array, and nothing in it can stop the scan early. Pass i therefore makes n - 1 - i comparisons whatever the values are, and the sum is n(n-1)/2. That is why best, average and worst are all O(n²): the data decides where the minimum sits, but never how long it takes to look. On eight elements the counter reads 28 comparisons for sorted input and for reversed input alike.
Movement is a different story. This version swaps only when the minimum is not already at position i, so the swap count is bounded by n - 1 and is often smaller. Sorted input of eight elements causes no swaps; reversed input causes four, because each swap puts one value at each end of the unsorted region. Compared with insertion sort, which shifts about n²/4 values on random data, selection sort writes a small fraction as much.
Space is O(1). The implementation keeps the index of the best candidate and also its value, so each comparison reads only the new element instead of re-reading the candidate. That is why the six-element trace shows 20 reads for 15 comparisons: one read per pass for the boundary element plus one per comparison.
Measured on this site
Exact counts from this site's own engine for 32 elements, using the same rules as the visualiser.
| Input | Comparisons | Writes | Swaps |
|---|---|---|---|
| Random | 496 | 0 | 27 |
| Already sorted | 496 | 0 | 0 |
| Reversed | 496 | 0 | 16 |
| Few distinct values | 496 | 0 | 22 |
When to use it
Use it when writing to memory is expensive and the array is small, because it makes at most n-1 swaps. It is also a clear first example of repeated selection.
Frequently asked questions
Why does selection sort do so few swaps?
Each pass finds the smallest remaining element with comparisons only and then swaps once, so the total is at most n-1 swaps.
Is selection sort stable?
No. The long-distance swap can move an element past an equal one.
Does it get faster on sorted input?
No. It always makes the same number of comparisons, about n²/2, whatever the order of the data.
When does selection sort beat insertion sort?
Mostly when writes are expensive and n is small. It performs at most n - 1 swaps, whereas insertion sort moves one value per inversion, about n²/4 on random data. On the six-element trace selection sort does 3 swaps and insertion sort does 13 writes. In comparisons insertion sort wins on random data, and by a lot on nearly sorted data, so the advantage exists only where moving costs more than comparing.
Can selection sort be made stable?
Yes, but not with a swap. Swapping [2a, 2b, 1] at positions 0 and 2 gives [1, 2b, 2a], and the two equal values have changed places. A stable variant instead shifts the block between the boundary and the minimum one cell to the right and writes the minimum into the gap. That keeps equal values in order, though it costs many more writes and removes the main reason to choose selection sort in the first place.
Background
Nobody is credited with inventing selection sort. Picking the smallest item, setting it aside and repeating is how people order cards or files by hand, and the method appears in the earliest discussions of sorting on machines. Knuth uses it in section 5.2.3 of his third volume as the base case of a family of selection methods.
Its main legacy is as the ancestor of better ones. Heap sort is selection sort with a priority queue to find the extreme value in logarithmic time instead of a full scan, and tournament-style methods follow the same idea. On its own it is rarely the best option, since insertion sort is faster on almost every input, but the small, predictable write count keeps it relevant when moving data is far more expensive than looking at it.
References
- Knuth, The Art of Computer Programming, vol. 3, section 5.2.3 (Sorting by Selection)
- Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms, chapter 2 exercises on selection sort
- Sedgewick and Wayne, Algorithms, 4th edition, section 2.1 (Elementary Sorts)
Compare with
| Algorithm | Average time | Worst time |
|---|---|---|
| Heap Sort | Θ( n log n ) | O( n log n ) |
| Cycle Sort | Θ( n2 ) | O( n2 ) |
| Pancake Sort | Θ( n2 ) | O( n2 ) |
| Best time | Ω( n2 ) | poor |
|---|---|---|
| Average time | Θ( n2 ) | poor |
| Worst time | O( n2 ) | poor |
| Worst space | O( 1 ) | good |
Properties
- Stable
- No
- In place
- Yes
- Adaptive
- No
- Comparison-based
- Yes