3.4.13 · D3Trees

Worked examples — Heap sort — in-place, O(n log n)

2,712 words12 min readBack to topic

This page is the "no surprises" drill for heap sort. We enumerate every kind of input the algorithm can meet, then work a real example for each. By the end you will have watched sift-down, build-heap, and full sorts run on tiny arrays, duplicate-heavy arrays, already-sorted arrays, reversed arrays, and even a one-element array — so nothing on an exam can catch you cold.


The scenario matrix

Cell Case class What could go wrong / what to prove Example
A Degenerate size: or Loops must not run; no crash Ex 1
B Tiny generic array (mixed order) Full build + full sort trace Ex 2
C Already ascending-sorted Build still does work; still Ex 3
D Reverse-sorted (descending) Input is almost a max-heap already Ex 4
E All keys equal (duplicates) No swaps; proves instability isn't triggered but also shows why stability fails Ex 5
F The Phase-2 shrinking-size trap Passing wrong n corrupts the sorted tail Ex 6
G Build-heap counting (limiting behaviour) Count actual sift steps vs the bound Ex 7
H Real-world word problem Top- / leaderboard framing Ex 8
I Exam twist: min-heap for descending order Choosing heap type to match desired order Ex 9

Every cell A–I is covered by exactly one example below.


Example 1 — Cell A: degenerate sizes


Example 2 — Cell B: full trace on a tiny mixed array


Example 3 — Cell C: already ascending-sorted input


Example 4 — Cell D: reverse-sorted input


Example 5 — Cell E: all keys equal + why heap sort is unstable


Example 6 — Cell F: the shrinking-size trap


Example 7 — Cell G: build-heap by literal step-counting


Example 8 — Cell H: real-world word problem


Example 9 — Cell I: exam twist — min-heap to sort descending


Recall checkpoint

Recall Does already-sorted input make heap sort faster?

No — Phase 1 still does swaps and Phase 2 always runs extractions. Heap sort is on every input (see Ex 3). Compare Quicksort which degrades on sorted input, and Introsort which switches to heap sort to avoid that.

Recall What size must you pass to sift-down in Phase 2?

The shrinking end, not len(A) — otherwise the sorted tail leaks back into the heap (Ex 6).

Recall Max-heap gives which sort order, min-heap gives which?

Max-heap → ascending; min-heap → descending (Ex 9). The extreme gets parked at the far end each round.

Which cell covers all-equal keys and instability?
Cell E — Example 5, [7,7,7,7].
Which cell proves build-heap is not by counting?
Cell G — Example 7 (4 swap-moves for ).
For descending order with only a min-heap available, what do you park where?
Park the extracted minimum at the tail each round → tail fills descending.

Connections

  • Heap sort — in-place, O(n log n) — the parent this drill supports.
  • Binary Heap — the structure every example manipulates.
  • Priority Queue — Example 8's repeated max-access pattern.
  • Merge Sort — use it instead when stability matters (Example 5).
  • Quicksort — the sorted-input contrast in Example 3.
  • Introsort — falls back to heap sort to guarantee worst case.