3.6.5 · D1Sorting & Searching

Foundations — Heap sort — O(n log n), in-place, not stable

1,828 words8 min readBack to topic

Before you can read the parent note, you need to see the objects it talks about. This page builds every one of them from nothing. Take them in order — each rests on the one above it.


1. An array — the raw material

We write the array as , and the value in box number as .

Figure — Heap sort — O(n log n), in-place, not stable
  • = the length of the array = how many boxes there are.
  • The valid indices are therefore . The last index is , not — box does not exist. This off-by-one is the single most common bug source, so we name it now.

2. A binary tree — the shape we imagine

Figure — Heap sort — O(n log n), in-place, not stable

Why does the topic need a tree at all? Because a plain array has no notion of "bigger than my neighbours above." The tree gives us a direction — up — so we can talk about a single biggest value floating to the top.


3. "Complete" — the trick that lets a tree live in an array

Figure — Heap sort — O(n log n), in-place, not stable

Read the figure top-to-bottom, left-to-right, writing each node into the next array box. The tree and the array are now the same thing, viewed two ways. That equality is the whole reason heap sort is in-place (uses no extra array).


4. The index formulas — turning "up/down" into arithmetic

Now that the tree lives in the array, "go to my child" must become a calculation on the index number. For a node at index (0-indexed, complete tree):

Two new symbols appear here — earn them before using them:

  • and : plain multiplication and addition. Nothing hidden.
  • — the floor brackets.
Figure — Heap sort — O(n log n), in-place, not stable

5. "" and the heap property — the ordering rule

Figure — Heap sort — O(n log n), in-place, not stable

6. swap and sift-down — the verbs

  • swap: exchange the contents of two boxes. Box 's value goes to box and vice-versa. This is how heap sort moves data without a second array.
  • sift-down (a.k.a. heapify): if a node's value is too small for its position, keep swapping it downward with its larger child until it sits where the heap rule holds. The parent note's inner loop is this verb.

7. and — the cost language


Prerequisite map

Array indexed from 0

Complete binary tree

Binary tree root parent child

Index formulas 2i+1 2i+2 floor

Floor rounds down

Greater or equal

Max-heap property

Sift-down and swap

Log n height gives O n log n

Heap sort


Equipment checklist

Cover the right side and answer each aloud before opening the parent note.

In a 0-indexed array of length , what is the last valid index?
What does "complete" buy us that lets a tree live in a flat array?
No gaps, so position alone encodes parent/child — no pointers, no extra memory.
Left child of node ?
Right child of node ?
Parent of node ?
Why is there a floor in the parent formula?
Division can give a fraction, but both children must map back to the one whole-numbered parent box, so we round down.
What is ?
State the max-heap property in words.
Every parent is greater than or equal to both of its children.
Where does the maximum value always sit in a max-heap?
At the root, index .
Is a heap the same as a sorted array?
No — only parent-beats-child holds; siblings are unordered.
What does sift-down do?
Sinks a too-small node downward by swapping with its larger child until the heap rule holds.
What does count here?
The number of levels (height) of a complete tree with nodes.
Why is heap sort ?
crownings, each ending in a sift-down of cost .
Recall Quick self-quiz

For , how tall is the heap (levels below root)? ::: . Node 's parent? ::: .