Card 04/ 11

ConceptDifficulty: Intermediate3 min

Selection Sort: Taking the Smallest of What Remains

Scan the unsorted part for its minimum, then swap it to the front of that part.

Selection sort orders an array by scanning the unsorted part for its smallest item and swapping that item to the front of the unsorted part, so the sorted part grows from the left by one item per pass. This card traces it, counts its comparisons, and shows why it is not stable.

Pass i starts with min at index i and walks the rest of the array, moving min to any item smaller than the one it marks. When the scan ends, min marks the smallest unsorted item, and one swap moves it to index i. If it is already there, nothing moves. The trace sorts 6 2 9 4 1 7 3, with i on the place being filled and min on where the smallest item was found.

Selection sort on seven numbers, one frame per pass

Input6 2 9 4 1 7 3, indices 0 to 6

  1. Pass 1: place 1

    1. 1i
    2. 2
    3. 9
    4. 4
    5. 6min
    6. 7
    7. 3

    A scan of all seven finds 1 at index 4, and one swap sends 6 there and 1 to the front.

  2. Pass 2: place 2

    1. 1
    2. 2imin
    3. 9
    4. 4
    5. 6
    6. 7
    7. 3

    2 is already the smallest of what remains, so min stays at i and nothing moves.

  3. Pass 3: place 3

    1. 1
    2. 2
    3. 3i
    4. 4
    5. 6
    6. 7
    7. 9min

    3 is found at index 6, and swapping it with 9 sends 9 to the end.

  4. Pass 4: place 4

    1. 1
    2. 2
    3. 3
    4. 4imin
    5. 6
    6. 7
    7. 9

    4 is the smallest of 4 6 7 9 and already in place.

  5. Pass 5: place 6

    1. 1
    2. 2
    3. 3
    4. 4
    5. 6imin
    6. 7
    7. 9

    6 is the smallest of 6 7 9 and already in place.

  6. Pass 6: place 7

    1. 1
    2. 2
    3. 3
    4. 4
    5. 6
    6. 7imin
    7. 9

    One comparison, 7 with 9, finishes the sort: 21 comparisons and two swaps in all.

ResultSorted after 6 passes: 21 comparisons, 2 swaps

The array was sorted after pass 3, and the sort went on scanning anyway. It has no way to stop early: the smallest item is known only once every unsorted item has been looked at. Pass 1 compares 6 times, pass 2 compares 5 times, and so on down to 1, so the total is n(n − 1)/2 comparisons on every input of n items, 21 for seven. The swaps are another matter. There is at most one per pass, so at most n − 1 in all, and this run needed two.

That swap is also why selection sort is not stable. It moves the item at the front of the unsorted part to wherever the minimum was found, which can be far to the right, jumping over every item in between. If one of those items has the same key as the item jumping, the two change order, and the sort has broken the order equal items arrived in. Bubble sort and insertion sort never move an item past an equal one, which is the difference.

The costs, for an array of n items, are these.

Selection sort on an array of n items
Every case
O(n²)
n(n − 1)/2 comparisons whatever the order
Swaps
At most n − 1
one per pass, none when the minimum is in place
Extra space
O(1)
swaps inside the array
Stable
No
a swap can carry an item past an equal one

Selection sort's time is fixed by n alone, so it is never faster on easier input. Its one strength is the swap count: when writing an item somewhere costs far more than comparing two, as with large records copied by value, n − 1 swaps can matter more than n²/2 comparisons.