learn/Algorithms/Selection Sort
ƒ(x) SortingCore interactive

Selection Sort

Repeatedly pick the minimum, place it at the front.

selection_sort
step 1 / 67
44
8
18
20
88
94
64
34
50
94
94

Find the minimum of the unsorted region.

comparingmovingsorted
speed1×

How it works

Selection sort scans for the smallest remaining element and swaps it into place, growing a sorted prefix one element at a time. It always does O(n²) comparisons but only O(n) swaps — useful when writes are expensive.

Mental models

  • Minimizes the number of swaps — at most n−1.
  • Comparisons are fixed regardless of input order.

Complexity

Best
O(n²)
Average
O(n²)
Worst
O(n²)
Swaps
O(n)
Space
O(1)