Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Selection Sort

Selection sort is a comparison-based sorting algorithm that repeatedly scans the unsorted portion of the array for its minimum value and swaps that value into the first unsorted slot. It runs in O(n²) time in every case, sorts in place with O(1) extra space, and performs at most n − 1 swaps.

Think of a coach lining up players by height with a clipboard: walk the remaining line, note the shortest player, and move that one person to the front of the sorted group. Every pass scans the entire unsorted group, so comparisons remain high while swaps stay rare.

How Selection Sort Works

Selection sort uses two nested loops and a running minimum index. The outer loop counts the pass index i from 0 to n − 2; slot i is the next position to fill. At the top of each pass, min_idx is set to i. The inner loop scans j from i + 1 to n − 1, updating min_idx = j whenever arr[j] < arr[min_idx].

When the scan ends, at most one swap moves arr[min_idx] into arr[i]. The guard if min_idx != i leaves a slot alone when it already holds its minimum.

The invariant is that after pass i, the first i + 1 cells hold the i + 1 smallest values in sorted order. The sorted region grows on the left, opposite to bubble sort, which settles its suffix on the right.

Selection Sort Step by Step

The default input is [25, 49, 25, 64, 12, 37, 15], which includes two 25s so you can observe stability.

Pass 1 (i = 0): min_idx starts at 0 (the first 25). Scanning right, 49 is larger; the second 25 ties, and strict < does not update on a tie, so min_idx stays at 0; 64 is larger; 12 at index 4 becomes the new minimum; 37 and 15 are larger. Swapping arr[0] with arr[4] produces [12, 49, 25, 64, 25, 37, 15], and 12 turns green in its final position. The two 25s entered at indices 0 and 2 and are now reversed because that swap moved the first 25 past the second. That move shows why selection sort is unstable.

Pass 2 (i = 1): min_idx starts at 1 (value 49). The scan passes 25, 64, the other 25, and 37 until 15 at index 6 becomes the minimum. One swap gives [12, 15, 25, 64, 25, 37, 49], moving 49 from index 1 out to index 6.

Pass 3 (i = 2): the smallest remaining value is the 25 already sitting at index 2, so min_idx never moves and nothing swaps. A pass can finalize a slot without modifying the array.

The remaining passes follow the same pattern of one full scan and at most one swap: pass 4 pulls the second 25 down to index 3 ([12, 15, 25, 25, 64, 37, 49]), pass 5 selects 37 ([12, 15, 25, 25, 37, 64, 49]), and pass 6 selects 49 ([12, 15, 25, 25, 37, 49, 64]), completing the sort.

In the animation, watch the min pointer track the smallest bar across the unsorted region while j scans. A pass swaps at most once; when the minimum is already at i, the guard skips the swap. Those occasional long-distance moves separate selection sort from bubble sort's sequence of neighbor swaps.

Time and Space Complexity

CaseTime
BestO(n²)
AverageO(n²)
WorstO(n²)

Space is O(1) because selection sort runs in place. The animated version uses the standard guarded form (if min_idx != i), so already-placed minimums skip their swap.

The notable figure is the swap count: O(n), at most one swap per pass, against n(n − 1)/2 comparisons in every case. When moving an element costs far more than comparing two (large records, slow storage), selection sort can outperform bubble sort and insertion sort despite having the same quadratic comparison bound.

The tradeoff is that selection sort is not adaptive. An already sorted input still runs every pass and every comparison.

Edge Cases

  • Empty or one-element array. The outer loop runs zero times.
  • Already sorted input. Every pass finds the minimum already at i, so the guard skips every swap, while comparisons still cost O(n²).
  • All equal values. arr[j] < arr[min_idx] never fires, min_idx never moves, and no swaps occur.
  • Duplicates. Selection sort is not stable, as the default run shows: the two 25s enter first-then-second and leave reversed because pass 1's swap moves the first one past the second. In miniature, [2a, 2b, 1] sorts to [1, 2b, 2a]. When equal records must keep their input order, selection sort is unsuitable.

Common Mistakes

  • Forgetting to reset min_idx = i at the top of each pass. Without the reset, min_idx carries over the previous pass's index and scans or swaps from the wrong region.
  • Comparing against arr[i] instead of arr[min_idx]. Losing the running minimum causes swaps with the first smaller value encountered, producing a different and wrong algorithm.
  • Swapping unconditionally. Without the min_idx != i guard, the loop performs no-op swaps and obscures the at-most-one-swap-per-pass property.
  • Assuming stability. Code that must keep equal records in order needs insertion sort, merge sort, or Timsort instead.

A few lessons that share the same technique or lead into the next idea.

Premium players need paid access. Every Explain doc is free.

Browse topic

Embed this lesson in a blog, docs, or course?

Found a mistake or something unclear?