Skip to content
SimpleScope

Sorting · 04 / 19

Selection sort visualization

Selection sort repeatedly selects the smallest remaining element and moves it to the end of the sorted prefix.

Time: O(n²) in every case. Space: O(1). What Big-O notation means.

Last updated

About this lesson
Concept it teaches
Picking the minimum, comparing costs
Canonical source
Knuth, TAOCP vol. 3, section 5.2.3
Builds on
Bubble sort

I already know this: go to the check

See

+10 XP

Watch the real code run, one step at a time. Change the input and see what happens.

Predict first. Before you press Play, guess what the first comparison will be.

Run Selection sort

Comparisons
0
Writes
0
Step
1/52
  • Comparing
  • Swapped
  • Done
  • Pivot

Array. index 0: 5; index 1: 3; index 2: 8; index 3: 1; index 4: 9; index 5: 2; index 6: 7.

Sort 7 numbers by picking the smallest remaining one each time.

Sort 7 numbers by picking the smallest remaining one each time.

Step 1/52comparisonwrite

With focus on the player: Space play · ← → step · Home End jump

Try your own input

Up to 16 whole numbers from -99 to 99, separated by commas or spaces.

A typical case: each pass finds the minimum and swaps it into place.

Curious how it compares? Race selection sort against another sort.

Understand

+15 XP

The idea in plain words: why it works, what it costs, when to use it. One question at the end.

Selection sort is a sorting algorithm that builds the sorted list from the left. On each pass it scans the unsorted part to find the smallest value and swaps it into the next free position.

How it works

  1. Treat the whole array as unsorted. Set i = 0.
  2. Scan indexes i to the end and remember the position of the smallest value.
  3. Swap that value with the one at index i, unless it is already there.
  4. Move i one step to the right and repeat until only one element is left.

A worked example of selection sort

Sort [5, 2, 6, 1, 4, 3]. Each pass finds the smallest value in the unsorted part and puts it at the front of that part:

  1. Smallest of indexes 0 to 5: 1 at index 3 (5 comparisons). Swap it with index 0: [1, 2, 6, 5, 4, 3].
  2. Smallest of indexes 1 to 5: 2 at index 1 (4 comparisons). Already in place: [1, 2, 6, 5, 4, 3].
  3. Smallest of indexes 2 to 5: 3 at index 5 (3 comparisons). Swap it with index 2: [1, 2, 3, 5, 4, 6].
  4. Smallest of indexes 3 to 5: 4 at index 4 (2 comparisons). Swap it with index 3: [1, 2, 3, 4, 5, 6].
  5. Smallest of indexes 4 to 5: 5 at index 4 (1 comparison). Already in place: [1, 2, 3, 4, 5, 6].

In total: 15 comparisons and 3 swaps. The comparisons would be the same for any order of these six values; only the swaps vary. The last value, 6, needs no pass of its own: when one element is left it must already be in place.

Selection sort pseudocode

Indexes start at 0, as in the code of this lesson. A is the array and n is its length.

SELECTION-SORT(A, n)
1  for i = 0 to n - 2
2      min = i
3      for j = i + 1 to n - 1
4          if A[j] < A[min]
5              min = j
6      if min != i
7          exchange A[i] with A[min]

The inner loop always runs to the end of the array, which is why the comparison count never changes. The test on line 6 only skips a pointless swap.

Time and space complexity

Finding the minimum of m values takes m - 1 comparisons, and it always scans the whole unsorted part, whatever the order of the input. Adding that up gives n(n - 1)/2 comparisons every time.

Complexity
Best case timeO(n²)
Average case timeO(n²)
Worst case timeO(n²)
Extra spaceO(1)
  • Best, O(n²): even a sorted array needs the full scan on every pass.
  • Average, O(n²): the comparison count does not depend on the order of the data.
  • Worst, O(n²): the same number of comparisons as the other cases.
  • Space, O(1): it sorts in place.

What makes it different is the number of swaps: at most one per pass, so at most n - 1 in total. Bubble sort can swap on almost every comparison.

Stability

This version is not stable. Swapping the minimum into place can jump an element over another one with the same value, changing their relative order. For example, sorting [2a, 2b, 1] swaps 2a with 1 and leaves 2b before 2a.

When to use it, and when not to

Use it when writes are much more expensive than comparisons, because it makes at most n - 1 swaps. Otherwise insertion sort is usually the better simple choice, since it adapts to sorted input and selection sort never does. For large data, use an O(n log n) algorithm.

Where selection sort is used

Like bubble sort, it is seldom the final choice in production code. Its three useful roles are about what it does well or what it leads to:

  • Media where writes are costly. Writing to flash memory wears the cells, so an algorithm that never makes more than n - 1 swaps, whatever the order of the input, can be worth its O(n²) comparisons on small data.
  • Taking the smallest values only. Stop after k passes and the first k positions hold the k smallest values in order, after roughly k × n comparisons. This beats a full sort when k is tiny.
  • The idea inside heap sort. Heap sort is selection sort that finds the next value with a heap in O(log n) time instead of a scan. Knuth presents it in section 5.2.3, "Sorting by selection", for that reason.

Common mistake and edge cases

Related algorithms and variations

A bidirectional version finds both the smallest and the largest value in each scan and fills both ends, which halves the number of passes. A stable version shifts the elements between the two positions instead of swapping, which keeps equal values in order but costs more writes, so it loses the algorithm's one advantage.

The closest relatives are bubble sort (bubble sort vs selection sort) and insertion sort (selection sort vs insertion sort). To keep the idea and drop the O(n²), compare it with heap sort in selection sort vs heap sort.

One question to finish

What is the largest number of swaps selection sort ever makes on n elements?

Predict

+20 XP

The run stops and asks what happens next. The real run says if you were right.

Practice

+30 XP

Order the lines, fill in the gap, trace it by hand. The real run corrects every answer.

Run it by hand

Run selection sort yourself. Make each decision the algorithm makes; the real run checks every move.

Check yourself

Four questions from memory. They also join your daily review. Questions return on a spaced schedule: right answers come back later, misses come back tomorrow.

Compare selection sort