You just watched selection sort. On every pass it finds the smallest value in the unsorted part and swaps it to the front of that part. The sorted part on the left grows by one each pass.

Quick check

Quick check: In pass 1 the animation above had 10 bars. How many compares did it make?

One pass, on four marks#

Four students scored 72, 45, 60 and 38. Each position in the list is a slot, and slot 0 is index 0. Pass 1 has to fill slot 0. It scans all four and finds the smallest, 38, in slot 3.

724560380123
marks before pass 1

One swap moves 38 to slot 0 and sends 72 to where 38 was. Slot 0 is now final. Nothing will ever move it again.

384560720123
marks after pass 1

Quick check

Quick check: The list is now [38, 45, 60, 72]. Pass 2 fills slot 1. What does it do?

The code#

m holds the slot number (the index) of the smallest value found so far, not the value itself. You need the slot to do the swap. Watch passes 2 and 3: they scan, but nothing moves.

Selection sort, line by lineline 1
  1. a = [72, 45, 60, 38]
  2. n = len(a)
  3. for i in range(n - 1):
  4. m = i
  5. for j in range(i + 1, n):
  6. if a[j] < a[m]:
  7. m = j
  8. a[i], a[m] = a[m], a[i]
  9. print(a)

Variables

a

72456038

Four students, four marks. Sort them low to high.

Step 1 of 30

The outer loop runs only n - 1 times. Once the first n - 1 slots hold the smallest values, the last value must be the largest, so it is already in place.

Sorted input does not help#

This is the same code, but now it runs on a list that is already sorted, and it counts the compares. Drag n. Then set n to 1 and look at what happens.

Counting compares on sorted inputline 1
Try another n
  1. n = 4
  2. a = list(range(1, n + 1))
  3. count = 0
  4. for i in range(n - 1):
  5. m = i
  6. for j in range(i + 1, n):
  7. count += 1
  8. if a[j] < a[m]:
  9. m = j
  10. a[i], a[m] = a[m], a[i]
  11. print(count, "compares")

Variables

n
4 (just changed)

The list 1 to n is already sorted. Drag n, try n = 1.

Step 1 of 35

The scans always have lengths (n - 1) + (n - 2) + ... + 1. There are n - 1 terms. Pair the first with the last (n - 1 + 1 = n), and you get (n - 1)/2 pairs of n each, which is n(n - 1)/2. With n = 1, range(0) is empty, so the code makes no pass and no compares.

Quick check

Quick check: A sorted list of 1,000 marks. Bubble sort with early exit makes 999 compares and stops. How many does selection sort make?

At most n - 1 swaps#

Each pass makes at most one swap, so a full sort makes at most n - 1 swaps. Bubble sort only swaps neighbours, and each swap fixes exactly one pair that is out of order, so it swaps once per such pair. On the opener's 10 values, that is a big gap:

On [5, 2, 8, 1, 9, 3, 10, 4, 6, 7]Bubble sortSelection sort
Values actually swapped177
Most swaps possible, n = 10459

Play bubble sort below and watch how often values change places.

Visualizer

Bubble Sort

Open full →

The swap count matters when writing is costly. Flash memory, like a pen drive or your phone's storage, wears out a little on every write, so fewer swaps means less wear. If you put if m != i: on the line before a[i], a[m] = a[m], a[i] (and indent the swap), a value already in place is not even written back to itself.

It is not stable#

A sort is stable if equal values keep their original order. Asha and Ravi both scored 72, and Asha comes first in the list. Watch what one long swap does to them.

Selection sort on equal marks

marks

slotnamemarks
0Asha72
1Ravi72
2Meena60
  • Same marks

Start: Asha and Ravi both have 72. Asha comes first.

Step 1 of 4

One swap sent Asha from slot 0 to slot 2, past Ravi. The two 72s changed order, so selection sort is not stable.

The cost at a glance#

CaseInput that causes itComparesTime
BestAlready sortedn(n - 1)/2O(n²)
AverageRandom ordern(n - 1)/2O(n²)
WorstAny order, e.g. reversedn(n - 1)/2O(n²)

Extra space is O(1), because it needs only i, j and m. It sorts in place, inside the same list. It is not stable. Use it when writes cost far more than reads, or when n is tiny. For everyday sorting, insertion sort or Python's built-in sorted() is faster.

One last check#

Quick check

Quick check: Selection sort on [30, 10, 20]. How many compares and how many real swaps?

Next you play selection sort and write it yourself, then meet insertion sort, which also grows a sorted left part but is stable and can stop early on nearly sorted input.