AlgoScope

Selection Sort

Scan for the smallest value left, then swap it home. At most n-1 swaps.

Sorting, algorithm, beginner
Swapping 1 from index 1 with 5 at index 0. Indices 0 to 0 are sorted.sorted 0-010514223843576imin
Swapping 1 from index 1 with 5 at index 0. Indices 0 to 0 are sorted.sorted 0-010514223843576imin

Selection Sort: Seven values

Step, step 9 of 35

Swap 1 into index 0. The sorted prefix grows.

Cost

Best
O(n^2)
Average
O(n^2)
Worst
O(n^2)
Space
O(1)

Always n(n-1)/2 comparisons. At most n-1 swaps.

Properties

  • Yes: Comparison based
  • No: Stable
  • Yes: In place
  • No: Adaptive
  • No: Online