3.4.11 · D3Trees

Worked examples — Heapify — bottom-up O(n) build

3,358 words15 min readBack to topic

This page is the "throw everything at it" companion to the parent Heapify note. There we derived why bottom-up build is . Here we drill the algorithm by hand across every kind of input it could ever meet — tiny arrays, already-sorted arrays, all-equal arrays, the empty degenerate case, a real-world word problem, and an exam trap.

Before we start, one reminder in plain words. A ==heap== is an array we pretend is a filling-left-to-right binary tree (a Complete Binary Tree). For a max-heap every parent is its two children; for a min-heap every parent is its children. siftDown(i) takes the value at slot i and lets it sink by repeatedly swapping with its larger (max-heap) child until it is bigger than both children or hits the bottom. Bottom-up build calls siftDown on every non-leaf, from index up to . Every symbol you meet below was defined here or in the parent — if you forget one, glance back.

Three short-hand names appear in every step below, so let us pin them down here on this page so you never have to hunt:

The index map we lean on constantly:


The scenario matrix

Heapify has fewer "quadrants" than trigonometry, but it still has genuinely distinct shapes of input. If a worked-examples page skips even one of these, you will one day hit a case you were never shown. Here is the full grid.

Cell What makes it distinct Covered by
A. Generic max-heap Random values, several real swaps, a cascading sink Ex 1
B. Generic min-heap Same algorithm, flipped comparison Ex 2
C. Already a valid heap Every siftDown breaks immediately — best case Ex 3
D. Reverse-sorted input Worst shape: root value is smallest, must sink fully Ex 4
E. All-equal elements Ties everywhere — must confirm no infinite loop / no wasted swaps Ex 5
F. Even with a lone left child The "right child out of bounds" guard actually fires Ex 6
G. Degenerate sizes and : the loop never runs — why that's correct Ex 7
H. Real-world word problem Priority-scheduling story mapped onto a heap Ex 8
I. Exam twist "How many swaps total?" — probes the intuition, not just the answer Ex 9

We now walk one example per cell (Ex 1 doubles as the cascade demo with a figure).


Cell A — generic max-heap, with a cascade


Cell B — generic min-heap (same algorithm, flipped test)


Cell C — input that is already a valid heap (best case)


Cell D — reverse-sorted, the worst shape


Cell E — all elements equal (tie handling)


Cell F — even , a node with only a left child


Cell G — degenerate sizes and


Cell H — a real-world word problem


Cell I — the exam twist


Recall Which cell was hardest for you?

Best case (already a heap) ::: Ex 3 — every siftDown early-exits, 0 swaps. Worst shape (ascending input) ::: Ex 4 — the root value cascades a full height. Ties / infinite-loop guard ::: Ex 5 — strict > means equal children never swap. Out-of-bounds right child ::: Ex 6 — even , the r < n guard fires and is skipped safely. and ::: Ex 7 — start index , loop never runs.

Related: Binary Heap · Complete Binary Tree · Heapsort · Priority Queue · siftUp vs siftDown · Amortized Analysis · Geometric Series