Interactive demo
Selection Sort Visualization
Selection sort searches the smallest of the remaining elements and swaps it to the front, one position after the other. It needs few writes, but always O(n²) comparisons.
Whatever the starting order: selection sort always needs the same 55 comparisons for 11 elements. With “Few unique values” it shows that it is not stable.
Playback
- Comparisons
- 0
- Writes
- 0
Array
Legend
- Unsorted element
- Smallest so far
- Compared element
- Final position
Runtime and properties
Best case
O(n²)
Even a sorted array is searched completely for each next minimum.
Average case
O(n²)
Always n(n − 1)/2 comparisons, independent of the input.
Worst case
O(n²)
The same n(n − 1)/2 comparisons, but never more than n − 1 swaps.
Extra memory
O(1)
Only the position of the current minimum is kept.
Stable
No
Swapping the minimum to the front can jump over an equal element.
In-place
Yes
Elements are only swapped within the array.
n is the number of elements. O(n log n) is the best a sorting algorithm based on comparisons can achieve.
Compared with the other sorting algorithms
| Algorithm | Best case | Average case | Worst case | Extra memory | Stable | In-place |
|---|---|---|---|---|---|---|
| Bubble sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Selection sort this page | O(n²) | O(n²) | O(n²) | O(1) | No | Yes |
| Insertion sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Shell sort | O(n log n) | ≈ O(n1.25) | O(n1.5) | O(1) | No | Yes |
| Tree sort | O(n log n) | O(n log n) | O(n²) | O(n) | Yes | No |
| Tournament sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Heapsort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Yes |
| Smoothsort | O(n) | O(n log n) | O(n log n) | O(1) | No | Yes |
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Patience sort | O(n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Timsort | O(n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Block sort | O(n) | O(n log n) | O(n log n) | O(1) | Yes | Yes |
| Quicksort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | Yes |
| Introsort | O(n log n) | O(n log n) | O(n log n) | O(log n) | No | Yes |
| Fluxsort | O(n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Crumsort | O(n) | O(n log n) | O(n log n) | O(log n) | No | Yes |
1. Search the minimum
Starting at the front of the unsorted part, every remaining element is compared with the smallest one found so far.
2. Swap it to the front
The smallest element is swapped to the front of the unsorted part. There it is at its final position, and the unsorted part shrinks by one.
3. Few writes, many comparisons
Selection sort never needs more than n − 1 swaps, which helps when writing is expensive. But it cannot stop early: even a sorted array costs n(n − 1)/2 comparisons.