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.
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
- Find the minimum of the rest.
- Swap it into place.
- Always O(n²) comparisons.
Code examples
Selection sort
[11, 12, 22, 25, 64]
Watching the sorted part grow
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
(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:
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
Prefer
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
- 1Use partial selection sort to find the three smallest numbers in a list.
- 2Show 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?