Step 0 / 0 · Comparisons: 0 · Swaps: 0
How this algorithm works
How it works
- Start with an unsorted array; the sorted region is initially empty.
- Scan the unsorted region to find its smallest element.
- Track the index of the smallest element seen so far as you scan.
- Once the scan finishes, swap that minimum into the front of the unsorted region.
- The sorted region now extends by one element.
- Repeat until only one element remains in the unsorted region.
Key concepts
- Comparison-based sorting
- Not stable (a swap can move an equal element past another)
- In-place (O(1) extra space)
- Always performs the same number of comparisons regardless of input order
When to use it
When memory writes are expensive, since selection sort performs at most n swaps (unlike bubble/insertion sort). Rarely used for large datasets due to O(n^2) comparisons in every case.
Did you know
Selection sort makes the fewest possible swaps (at most n-1) of any simple sorting algorithm, which made it attractive when writing to memory was far slower than reading it.