Start at the last parent
transcript
You have an array of values. You need a heap. Pushing one by one costs more. Start at the last parent, not at the root.
A leaf is a heap already. Going back, everything below is ready. So one sift fixes the parent. The build takes about two n steps.
The last parent is at n divided by two, minus one. Cells after it are leaves. Go back to index zero. Sift each parent down.
Six cells. The last parent is cell two. Seven swaps with its smaller child, three. Four swaps with one. Nine falls twice. Four swaps in all.
Pushing makes one and a half million swaps. The bottom-up build makes under a hundred thousand. Half of the cells are leaves, and a leaf never moves. So the build does far less work in total.
Go from the last parent. Sift each one down. Full breakdown in the lecture.