minimum
scanning
swapping
sorted
5
0
3
1
8
2
1
3
9
4
2
5
Find the minimum element in the unsorted portion. Swap it into position. Repeat.
Like finding the smallest card in your hand, placing it first, then the next smallest, and so on.
start
1 / 80
SPD0.5×
click to pause · hover for controls · fullscreen for more

Selection Sort

Repeatedly selects the smallest element

Visual by thisgirltech

BeginnerSorting

Selection sort has a simple and satisfying structure — it makes exactly one swap per pass, and after each pass one more element is permanently in its correct position. Watch the green sorted region grow one bar at a time, never touched again.

Each pass starts at the leftmost unsorted position and scans every remaining element to find the smallest. The yellow bar tracks the current best candidate. When the scan finishes, that yellow bar swaps with the first unsorted position — a single red flash — and the pass is done.

This animation also shows the cases where no swap is needed — when the element at the pass start is already the minimum, it stays put. Watch the swap counter: selection sort never makes more than n-1 swaps regardless of the input, which is why it is useful when writes are expensive.

Exactly n−1 swaps

No matter how disordered the input is, selection sort makes at most n−1 swaps — one per pass. This is fewer than any other O(n²) algorithm, making it useful when write operations are costly.

Always O(n²) comparisons

Unlike insertion sort, selection sort gets no benefit from nearly-sorted data. It always scans the entire unsorted region — pass 1 does n−1 comparisons, pass 2 does n−2, and so on. Total: n(n−1)/2.

Not stable

Swapping the minimum to the front can jump it past equal elements, changing their relative order. Example: sorting [3a, 3b, 1] gives [1, 3b, 3a] — the two 3s are reversed. This disqualifies selection sort where stability matters.

In-place, O(1) space

Like insertion sort and bubble sort, selection sort sorts in place using only a constant amount of extra memory — one variable to track the minimum index. No auxiliary array is needed.

TIP

Watch the Swaps counter — it barely moves. Selection sort finishes the entire 13-element sort with at most 12 swaps. Compare that to the Comparisons counter which grows rapidly — that is the O(n²) cost showing itself.

COLOUR LEGEND

Pass start
Scanning
Current min
Swapping
Sorted
Unsorted