Heap Sort
Build a Max-Heap, then Extract the Max
Heap Sort is a comparison sort that uses a binary heap structure. The core idea is simple: heapify the array into a max-heap, then repeatedly swap the root (largest) with the last element and heapify the reduced heap.
This page is designed as a teaching artifact: clear sections, progressive disclosure, and “what to watch for” cues so learners can connect the animation to the algorithm logic.
Overview
Heap Sort guarantees O(n log n) time and performs sorting in-place (constant extra memory). It’s a strong teaching algorithm because it shows how a data structure (heap) can power a sorting method.
This page teaches Heap Sort using a structured learning flow: Build Max-Heap → Repeated Extract-Max → Restore Heap. The UX goal is to make the algorithm feel “trackable” by showing learners: (1) where they are, (2) what changed, and (3) why the step matters.
The learning problem
Users & context
- Primary: beginners in data structures / algorithms who learn best with visuals.
- Secondary: instructors/TAs who need a clean artifact for demos and critique.
- Environment: classroom projection, self-study, or short lab sessions.
Goals & success criteria
- Explain the two phases: build heap and sort/extract.
- Predict what happens next (swap vs sift-down).
- Track the boundary between heap region and sorted region.
- Fewer “what just happened?” moments during demo.
- Students can label the phase correctly.
- Students can answer a 2–3 question checkpoint after the demo.
Demo / Media
Heap Sort Animation
This animation uses repeated Swap actions to visualize heapify operations and extraction.
In the animation, the array starts as [4, 3, 1, 5, 2], each value is drawn inside a square,
and swaps happen during heapify and extraction. :contentReference[oaicite:1]{index=1}
How the animation maps to the code
Swap()
and also swaps the array values to keep logic and visuals aligned. :contentReference[oaicite:3]{index=3}
heapify() is the “repair step”
Pseudocode
HEAP-SORT(A):
n = length(A)
// 1) Build a max-heap
for i = floor(n/2) - 1 down to 0:
HEAPIFY(A, n, i)
// 2) Extract max one-by-one
for end = n - 1 down to 1:
swap A[0], A[end] // move max to its final place
HEAPIFY(A, end, 0) // restore heap in the remaining prefix
HEAPIFY(A, heapSize, i):
largest = i
left = 2*i + 1
right = 2*i + 2
if left < heapSize and A[left] > A[largest]: largest = left
if right < heapSize and A[right] > A[largest]: largest = right
if largest != i:
swap A[i], A[largest]
HEAPIFY(A, heapSize, largest)
Complexity
Teaching flow (scaffolded)
UX/UI • Pedagogy • HCI
This page intentionally uses structure and visual cues to reduce cognitive load for beginners.
- Phase clarity: labels + section headers separate “Build Heap” from “Sorting.”
- Progress visibility: a clear boundary for heap region vs sorted region.
- Change emphasis: highlight only the elements that moved (swap + sift path).
- Consistent layout: same card structure across pages reduces reading friction.
- Scaffolding: teach heap property first; only then teach extraction loop.
- Chunking: one “meaningful unit” per step: swap → sift-down → done.
- Checkpoints: small questions to confirm understanding before moving on.
- Mental models: “root is max” + “sorted region grows” becomes predictable.
- Recognition over recall: labels and boundaries reduce memory load.
- Feedback: each step shows an immediate effect (progress + restored heap).
- Cognitive load control: avoid showing too many highlights at once.
Evaluation plan (lightweight but real)
- Identify phase: “Are we heapifying or sorting right now?”
- Predict next action: swap vs sift-down.
- Mark boundary: which part is sorted?
Iteration note: After the first test, I would refine labels, reduce highlight noise, and add a short “phase legend” if learners still confuse heapify vs sorting.