Loading lessons...
Selection Sort & Heap Sort
Selection Sort & Heap Sort
Selection sorts build the sorted output one position at a time by repeatedly selecting the smallest (or largest) of the remaining elements. Straight selection finds that element by scanning - O(n2). Heap sort finds it with a heap - O(n log n), still in place.
1. Selection Sort
Idea: in pass I, find the smallest element of the unsorted part A[I .. N-1] and swap it into position I. After pass I, the first I + 1 elements are sorted and in their final places.
Algorithm SELECTION_SORT(A, N)
for I ← 0 to N - 2 do
MIN ← I // assume A[I] is the smallest
for J ← I + 1 to N - 1 do
if A[J] < A[MIN] then
MIN ← J // remember the new smallest
if MIN ≠ I then
swap A[I] and A[MIN] // one swap per passTrace on 8 4 6 9 2 3 1
| Pass | Minimum of unsorted part | Array after the swap (sorted part in bold) |
|---|---|---|
| 1 | 1 | 1 4 6 9 2 3 8 |
| 2 | 2 | 1 2 6 9 4 3 8 |
| 3 | 3 | 1 2 3 9 4 6 8 |
| 4 | 4 | 1 2 3 4 9 6 8 |
| 5 | 6 | 1 2 3 4 6 9 8 |
| 6 | 8 | 1 2 3 4 6 8 9 |
Analysis
- Comparisons: (n-1) + (n-2) + … + 1 = (n(n-1) / 2) - always, whatever the input (21 for n = 7). So best, average and worst case are all Θ(n2); selection sort is not adaptive.
- Swaps: at most n - 1 - fewer than any other simple sort. Useful when writing data is expensive (e.g. flash memory).
- Space: O(1).
- Not stable: the long-distance swap can jump an element over an equal one. Example: 5a 5b 2 → pass 1 swaps 5a with 2 → 2 5b 5a.
2. Heap Sort
Selection sort spends O(n) per pass finding the maximum. A max-heap gives the maximum in O(1) and restores itself in O(log n). Heap sort (J. W. J. Williams, 1964) uses the array itself as the heap, so it needs no extra memory.
Two phases
- Build a max-heap from the array (bottom-up, O(n)).
- Repeat n − 1 times: swap the root (the maximum) with the last element of the heap; the maximum is now in its final position. Shrink the heap by one and reheap down from the root.
Algorithm HEAP_SORT(A, N)
for I ← (N div 2) - 1 downto 0 do // phase 1: build max-heap
REHEAP_DOWN(A, N, I)
for END ← N - 1 downto 1 do // phase 2: extract maxima
swap A[0] and A[END] // max goes to its final place
REHEAP_DOWN(A, END, 0) // heap is now A[0 .. END-1]Trace on 4 10 3 5 1 8 2
Phase 1 - build the max-heap. Non-leaf nodes are indices 2, 1, 0.
- i = 2 (3): children 8, 2 → swap with 8 → 4 10 8 5 1 3 2
- i = 1 (10): children 5, 1 → already larger, no change
- i = 0 (4): children 10, 8 → swap with 10 → 10 4 8 5 1 3 2; then 4's children 5, 1 → swap with 5 → 10 5 8 4 1 3 2
10
/ \
5 8
/ \ / \
4 1 3 2Phase 2 - extract the maximum repeatedly (heap part | sorted part):
| Step | Heap after reheap down | Sorted part |
|---|---|---|
| swap 10 with 2 | 8 5 3 4 1 2 | 10 |
| swap 8 with 2 | 5 4 3 2 1 | 8 10 |
| swap 5 with 1 | 4 2 3 1 | 5 8 10 |
| swap 4 with 1 | 3 2 1 | 4 5 8 10 |
| swap 3 with 1 | 2 1 | 3 4 5 8 10 |
| swap 2 with 1 | 1 | 1 2 3 4 5 8 10 |
Analysis
- Phase 1: O(n). Phase 2: n - 1 reheaps of O(log n) each → O(n log n).
- Best, average and worst case: O(n log n) - no bad inputs, unlike quick sort.
- Space: O(1) - in-place.
- Not stable (swapping the root with the last element jumps over equal keys).
- In practice slower than a good quick sort because it jumps around the array (poor cache use).
3. Selection Sort vs Heap Sort
| Basis | Selection sort | Heap sort |
|---|---|---|
| How the next element is chosen | Linear scan, O(n) | Heap root, O(log n) to restore |
| Time (all cases) | O(n2) | O(n log n) |
| Swaps | At most n - 1 | O(n log n) |
| Space | O(1) | O(1) |
| Stable | No | No |
| Use | Tiny arrays; when writes are expensive | Large arrays needing a guaranteed O(n log n) with no extra memory |
Key points to remember - Selection sort: select the minimum, swap it to the front; always (n(n-1) / 2) comparisons, at most n - 1 swaps, not stable. - Heap sort: build a max-heap (O(n)), then swap root with the last element and reheap down, n − 1 times. - Heap sort is O(n log n) in every case and in-place, but not stable. - 4 10 3 5 1 8 2 → max-heap 10 5 8 4 1 3 2.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Write the selection sort algorithm, trace it and find its complexity. (5 Marks)
- Write:
SELECTION_SORT, the pass table, (n(n-1) / 2) comparisons in every case, at most n - 1 swaps. - Q2. Explain heap sort with an example. Derive its time complexity. (10 Marks)
- Write: the two phases,
REHEAP_DOWN, the build-heap steps and extraction table for 4 10 3 5 1 8 2, and O(n) + O(n log n). - Q3. Why is heap sort preferred over selection sort for large inputs? Is either stable? (2.5 Marks)