Data Structures · Sorting

Selection sort

Selection sort builds a sorted list one element at a time: find the smallest value that is not yet in place, and swap it into the next position. It is one of the simplest sorting algorithms, which makes it a good place to start thinking about how sorts work and what they cost.

The idea

The array is split into two parts by a boundary that moves from left to right:

Each pass does the same three things:

  1. Find the minimum of the unsorted part, by scanning it from left to right.
  2. Swap that minimum with the first unsorted element.
  3. Move the boundary one place to the right. The swapped-in value is now sorted.

After n − 1 passes, only one element is left in the unsorted part, and it must be the largest, so the array is sorted.

A worked example

Sorting {64, 25, 12, 22, 11}, the array from the class code. Blue cells are sorted; the yellow cell is the minimum found in that pass.

Try it

The array

Sorted (final place) Being compared (j) Smallest so far (min_idx) Unsorted

Variables

What is happening

main.cpp

    This is the class code. The array in main() changes to match what you sort.

    How much work does it do?

    Comparisons. Pass 1 compares n − 1 pairs, pass 2 compares n − 2, and so on down to 1. That adds up to

    (n − 1) + (n − 2) + … + 1 = n(n − 1) / 2

    which is O(n²). The inner loop always runs to the end of the array, whatever the data looks like, so the count is exactly the same for sorted, random and reversed input. Selection sort is not adaptive: it gains nothing from data that is already in order.

    Swaps. There is at most one swap per pass, so at most n − 1 swaps in total, which is O(n). Few algorithms move data this little, which helps when writing to memory is expensive.

    Memory. It sorts in place, using only a few extra variables: O(1) extra space.

    The same n, three kinds of input:

    Strengths and weaknesses

    Strengths
    • Very simple to write and to reason about.
    • At most n − 1 swaps: good when moving data is costly.
    • Sorts in place with O(1) extra memory.
    • Fine for small arrays.
    Weaknesses
    • Always O(n²) comparisons, even on sorted input.
    • Too slow for large arrays; merge sort and quicksort are O(n log n).
    • Not stable: the long-distance swap can reorder equal values. Try the Equal values preset.

    Think first

    How many comparisons does selection sort make on an array of 6 values?

    5 + 4 + 3 + 2 + 1 = 15, which is 6 · 5 / 2. It is 15 no matter how the values are arranged.

    Why does the outer loop stop at i < n − 1 instead of i < n?

    When only one element is left in the unsorted part, it is the largest value, and it is already in the last position. There is nothing to compare it with.

    A sort is stable if equal values keep their original order. Why is selection sort not stable?

    The swap can jump a value over others. In 5a, 3, 5b, 2, the first pass swaps 2 with 5a, sending 5a behind 5b: 2, 3, 5b, 5a. The two 5s have changed order.

    What would you change to sort from largest to smallest?

    Change the comparison to arr[j] > arr[min_idx], so each pass selects the largest remaining value. (The variable would then be better named max_idx.)