Reading preferences
Concept

Selection Sort

Search for the smallest remaining number, put it in place, and repeat.

sorting, selection sort, minimum, unstable, comparisons

Select the next minimum

Imagine choosing books for a shelf. Look through all the books still on the table, choose the one with the smallest number sticker, and put it first. Then choose the smallest of those still on the table and put it second. Selection means choosing; the minimum means the smallest.

Selection Sort splits a list into a sorted prefix and an unsorted suffix. Find the smallest value in the suffix and swap it into the first unsorted position. Repeat until nothing remains to place.

Unlike Bubble Sort, selection does not move a value through every neighbour. It scans first, remembers the minimum's index, and then makes one placement.

Why learn it and where it is useful

Selection Sort answers a different question from Bubble Sort: instead of fixing neighbours, can we choose the next correct item directly? It teaches how to search a whole group while remembering the best candidate found so far.

When exchanges are costly and the list is small, making few exchanges can be useful. But Selection Sort still looks at many pairs of numbers, even if the list is already ordered. For large everyday software lists, use a suitable library sort rather than assuming this method is fastest.

Why the prefix stays correct

The prefix is the part on the left. Before each new choice, those left-hand items are already the smallest ones in the right order. The rest is the suffix, the part on the right that we still need to sort.

Before a pass, the prefix contains the smallest values in sorted order. The minimum of everything left is the next value that belongs in that prefix. Swapping it into position extends the invariant by one element.

Every pass reduces the unsorted region. Comparisons choose a value; the swap preserves the collection's elements and their multiplicities.

A worked example

PassChosen minimumAfter placement
Start—[5, 2, 4, 1]
11[1, 2, 4, 5]
22[1, 2, 4, 5]
34[1, 2, 4, 5]

This input happens to become sorted after the first swap. The implementation still scans the later suffixes; it does not infer completion from one lucky pass.

See the reasoning

Try Selection Sort one decision at a time

Goal: smallest → largest. Taller bars mean bigger numbers. This is a teaching demonstration, not live Python execution.

  • Compare
  • Move
  • Final place
  1. 5
    Slot 1Not final
  2. 2
    Slot 2Not final
  3. 4
    Slot 3Not final
  4. 1
    Slot 4Not final
  5. 3
    Slot 5Not final

Start

Our goal is smallest to largest. Each bar is one number; a taller bar means a larger number.

Python illustration

def selection_sort(values):
    for start in range(len(values) - 1):
        minimum = start
        for index in range(start + 1, len(values)):
            if values[index] < values[minimum]:
                minimum = index
        if minimum != start:
            values[start], values[minimum] = values[minimum], values[start]
    return values

print(selection_sort([5, 2, 4, 1]))

Prepared output — not live execution:

[1, 2, 4, 5]

Tracking the index instead of copying a suffix keeps auxiliary space constant. Empty and one-element lists need no passes. The function mutates and returns the original list.

Comparisons and swaps are different costs

Looking at two books and deciding which number is smaller is a comparison. Physically exchanging the books is a swap. We should count them separately: few swaps can still require lots of comparisons.

In the formulas below, n means the number of books. O(n²) means the work can grow roughly with the square of that number, even when the books start ordered.

For any input order, the comparison count is (n - 1) + (n - 2) + ... + 1 = n(n - 1)/2. Best, average, and worst time are O(n²). There are at most n - 1 swaps, and auxiliary space is O(1).

This distinction matters when moving a record is expensive: few swaps do not mean few comparisons. Selection Sort is still generally unsuitable for large arbitrary collections.

Why standard Selection Sort is not stable

Label equal keys to distinguish their original order: [2a, 2b, 1]. The first pass swaps 1 with 2a, yielding [1, 2b, 2a]. The two records with key 2 have reversed their relative order.

Even strict < in the minimum search cannot prevent this distant-swap effect. A different implementation can preserve stability by shifting records instead, but that is not the standard swapping algorithm shown here.

Compare this with Insertion Sort, which shifts larger values while preserving equal-key order.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does each pass add to the sorted prefix?
2. How does this implementation behave on an already sorted list?
3. Is the standard swapping implementation stable?
4. How many swaps can this implementation make for n elements?

References and further reading