DSA Notes
Master selection sort — the mechanism, step-by-step trace, formal analysis, the unique property of making at most n-1 swaps, why it is not adaptive, comparison with bubble and insertion sort, and when the swap-minimizing property makes it the right choice.
How Selection Sort Works
Selection sort divides the array into two parts: sorted (left) and unsorted (right). It repeatedly finds the minimum element in the unsorted portion and places it at the end of the sorted portion.
| Array | [64, 25, 12, 22, 11] |
| Pass 1: Find min in [64,25,12,22,11] | 11. Swap with position 0. |
| Pass 2: Find min in [25,12,22,64] | 12. Swap with position 1. |
| Pass 3: Find min in [25,22,64] | 22. Swap with position 2. |
| Pass 4: Find min in [25,64] | 25. Already at position 3 (no swap needed). |
| Done | [11, 12, 22, 25, 64] |
The Key Property — Minimum Number of Swaps
Selection sort makes at most n-1 swaps regardless of input. This is a unique property among O(n²) sorting algorithms.
| Pass 1: min=arr[0]=1, min_idx=0 == i=0 | NO SWAP |
| Pass 2: min=arr[1]=2, min_idx=1 == i=1 | NO SWAP |
| Total swaps | 0 |
| Pass 1: min=1 at idx 4, swap with idx 0 | [1, 4, 3, 2, 5] |
| Pass 2: min=2 at idx 3, swap with idx 1 | [1, 2, 3, 4, 5] |
| Total swaps | ≤ n-1 |
| Bubble Sort worst case | n(n-1)/2 swaps |
| Selection Sort worst case | n-1 swaps |
When does this matter? If swapping elements is very expensive (e.g., swapping large records on disk, or elements with complex constructors), selection sort's minimized swap count makes it preferable despite equal comparison count.
Complexity Analysis
All Cases — Θ(n²)
Selection sort has no early termination mechanism. Regardless of input:
- Inner loop always scans the full unsorted portion
- Total comparisons: (n-1) + (n-2) + ... + 1 = n(n-1)/2
| Best case | Ω(n²) — no way to terminate early |
| Average case | Θ(n²) |
| Worst case | O(n²) |
Unlike Bubble Sort and Insertion Sort, Selection Sort is NOT adaptive — it performs the same work on every input.
Is Selection Sort Stable?
The standard implementation is NOT stable:
| Input | [(3,'A'), (3,'B'), (1,'C')] (sort by number) |
| Pass 1: Find min of all | (1,'C') at index 2 |
| Result | [(1,'C'), (3,'B'), (3,'A')] |
| but now it's B then A | UNSTABLE |
You can make selection sort stable by replacing swaps with shifts (like insertion sort), but this increases the number of operations and removes the swap-minimizing advantage.
Properties Summary
| Property | Value |
|---|---|
| Stable | ❌ No (standard implementation) |
| In-place | ✅ Yes — O(1) auxiliary space |
| Adaptive | ❌ No — always O(n²) |
| Best case | Ω(n²) |
| Worst case | O(n²) |
| Average case | Θ(n²) |
| Swaps (worst) | n-1 — minimum possible |
| Comparisons | n(n-1)/2 always |
When to Use Selection Sort
Selection Sort is appropriate when:
- Write operations are expensive — e.g., writing to flash memory (each write degrades the memory; minimizing writes matters)
- Only n-1 writes allowed — systems with very limited write cycles
- Very simple implementation needed — embedded systems where code size matters
Selection Sort is inappropriate when:
- Performance matters for n > 100
- Stability is required
- Better algorithms (Insertion Sort, Quick Sort) have negligible overhead
Comparison with Other O(n²) Sorts
| All three | ~500,000 comparisons |
| Bubble Sort | up to 500,000 swaps (3 operations each = 1.5M writes) |
| Selection Sort | at most 999 swaps (3 operations each = ~3,000 writes) |
| Insertion Sort | up to 500,000 shifts (1 operation each = 500,000 writes) |
| If write cost >> compare cost | Selection Sort wins |
| If write cost ≈ compare cost | Insertion Sort wins (better adaptive behavior) |
Summary
Selection sort: find minimum, place it at front, repeat.
- Θ(n²) for all inputs — no adaptive behavior
- Maximum n-1 swaps — unique property, valuable when writes are costly
- Not stable — equal elements can change order
- Use case: Memory/hardware with expensive write operations
*Next Lesson: Insertion Sort*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Selection Sort — Complete Analysis with Swaps Minimization Property.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, sorting, selection, sort
Related Data Structures & Algorithms Topics