Skip to content

Module 18 · Algorithms and Problem Solving — topic 11 of 18

Selection sort

Sorting by repeatedly selecting the smallest remaining element: the algorithm, tracing each step, counting comparisons and swaps, O(n²) time regardless of input, minimal swaps and lack of stability.

Beginner 8 min read·Lesson 271 of 306 in Python Programming

Learning objectives

  • Implement selection sort
  • Trace the sorted portion growing
  • Compare its comparisons and swaps with bubble sort

Definition

Selection sort divides the list into a sorted part and an unsorted part. On each step it finds the smallest element of the unsorted part and swaps it into the next position of the sorted part.

Introduction

Arranging cards by repeatedly picking the lowest card from your hand and placing it next in a row — that's selection sort.

Why it matters

Selection sort makes very few swaps, which matters when writing data is expensive, and it shows clearly how 'find the minimum' becomes a sorting algorithm.

Detailed explanation

For position i from 0 to n − 2, find the index of the smallest element from i to the end, then swap it with position i.

After step i, the first i + 1 elements are sorted and in their final places.

It always performs n(n − 1)/2 comparisons, even on sorted data, so it is O(n²) in the best, average and worst cases.

It makes at most n − 1 swaps — fewer than bubble sort — which helps when moving data is costly.

The long-distance swaps can move equal elements past each other, so selection sort is not stable.

It sorts in place with O(1) extra memory.

Finding the k smallest items with k steps of selection is a useful partial version.

Like bubble sort, it is mainly educational; Python's sorted() is far faster.

Syntax

Python
def selection_sort(items):
a = list(items)
for i in range(len(a)):
smallest = i
for j in range(i + 1, len(a)):
if a[j] < a[smallest]:
smallest = j
a[i], a[smallest] = a[smallest], a[i]
return a
  • Find the minimum of the rest.
  • Swap it into place.
  • Always O(n²) comparisons.

Code examples

Selection sort

Python
def selection_sort(items):
a = list(items)
for i in range(len(a)):
smallest = i
for j in range(i + 1, len(a)):
if a[j] < a[smallest]:
smallest = j
a[i], a[smallest] = a[smallest], a[i]
return a
print(selection_sort([64, 25, 12, 22, 11]))
Output
[11, 12, 22, 25, 64]

Watching the sorted part grow

Python
a = [64, 25, 12, 22, 11]
for i in range(len(a) - 1):
smallest = min(range(i, len(a)), key=lambda k: a[k])
a[i], a[smallest] = a[smallest], a[i]
print(f"step {i + 1}: {a}")
Output
step 1: [11, 25, 12, 22, 64]
step 2: [11, 12, 25, 22, 64]
step 3: [11, 12, 22, 25, 64]
step 4: [11, 12, 22, 25, 64]

More examples

Comparisons and swaps

Python
def selection_stats(items):
a = list(items)
comparisons = swaps = 0
for i in range(len(a)):
smallest = i
for j in range(i + 1, len(a)):
comparisons += 1
if a[j] < a[smallest]:
smallest = j
if smallest != i:
a[i], a[smallest] = a[smallest], a[i]
swaps += 1
return comparisons, swaps
print(selection_stats(range(10)), selection_stats(range(10, 0, -1)), selection_stats([3, 1, 2]))
Output
(45, 0) (45, 5) (3, 2)

Step-by-step explanation

  • Sorted input still needs all 45 comparisons, but no swaps.
  • Reversed input of 10 items needs only 5 swaps, because each swap fixes two elements at once.
  • For three items there are always 3 comparisons.

Expected output

Running “Comparisons and swaps” prints exactly:

TextOutput
(45, 0) (45, 5) (3, 2)

Practical applications

  • Sorting where writes are expensive, such as some flash memory.
  • Picking the top few items with a partial selection.

Best practices

  • Track the index of the minimum, not its value.
  • Skip swaps when the minimum is already in place.
  • Use sorted() in real code.

Common mistakes

Swapping inside the inner loop

Avoid

for j in range(i + 1, n):
if a[j] < a[i]:
a[i], a[j] = a[j], a[i]

Prefer

Find the index of the minimum first, then swap once after the inner loop.

Swapping repeatedly turns it into a slower, different algorithm.

Important notes

  • Heap sort is a fast cousin of selection sort that finds each minimum with a heap in O(log n).

Key takeaways

  • Selection sort moves the smallest remaining element into place each step.
  • Always O(n²) comparisons; at most n − 1 swaps.
  • In place but not stable.

Try it yourself

  1. 1
    Use partial selection sort to find the three smallest numbers in a list.
  2. 2
    Show with an example that selection sort is not stable.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

2 questions test this exact topic. Answers and explanations are revealed after you submit.

  • AdvancedHow many comparisons does selection sort make on an already sorted list of 10 items?
  • AdvancedWhich of these sorts is NOT stable?
Practise this topic