Skip to content
← Back to algorithms

Step 0 / 0 · Comparisons: 0 · Swaps: 0

How this algorithm works

How it works

  1. Start with an unsorted array; the sorted region is initially empty.
  2. Scan the unsorted region to find its smallest element.
  3. Track the index of the smallest element seen so far as you scan.
  4. Once the scan finishes, swap that minimum into the front of the unsorted region.
  5. The sorted region now extends by one element.
  6. 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.