3.4.13 · D1Trees

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

1,713 words8 min readBack to topic

Before you can read the parent note, you need to own every symbol it throws at you. We build them one at a time, each on top of the last. Nothing appears before it is drawn.


1. What is an array? (the row of boxes)

The little number is called the index — it is just the label on the box, like a house number on a street. We start counting at , not . So the first box is , the second is , and so on. This "start at zero" habit is called 0-based indexing and every formula in the parent note assumes it.

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

Why an array and not a bag of numbers? Because a box has a fixed address. If I know box exists, I can jump straight to it — no searching. That instant jump ("random access") is the whole reason heap sort can be fast and use no extra memory.

Recall If

, what is ? ::: box holds , box holds , box holds . Counting starts at zero.


2. What is a tree? (boxes that point to boxes)

Picture a family tree drawn upside-down: the ancestor (root) is at the top, descendants fan out below.

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

The word binary just means "two" — at most two children each. See Complete Binary Tree for the exact shape we need, which we build next.

Recall What is a leaf?

A box with no children ::: it sits at the very bottom of the tree with nothing hanging below it.


3. The "complete" shape (no holes)

Why do we insist on this tidy shape? Because a tree with no holes can be flattened into an array with zero wasted boxes. Read the tree level by level, left to right, and drop each node into the next array box. That mapping is the bridge between "tree" (easy to think about) and "array" (cheap to store).

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

Look at the figure: the tree on the left and the row on the right hold the same numbers in the same slots. The parent calls this the "array form" of a heap.


4. The index arithmetic (parent ↔ child by formula)

Here is the payoff of the complete shape. Because there are no gaps, the position of any child or parent can be computed — no pointers, no arrows stored anywhere. For a box at index :

Two new symbols appear here, so we define them before using them:

Why floor? A parent is one box; two different children (left and right) both point back to it. Floor division squashes both "" answers onto the same whole number, so one formula recovers the parent no matter which child you started from.

Trace it in the figure of §3: box 's left child is , right child is . Box 's parent is . It closes the loop. ✔

Recall Right child of node

? ::: plug into .


5. The heap property (biggest floats up)

The symbol reads "at least as big as." Because every parent beats its children, and the chain of parents leads up to the root, the root box always holds the maximum of the whole array. That single fact is the engine of the entire sort.

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

Note what the heap property does not say: it says nothing about left-vs-right, and nothing about cousins. A heap is only loosely sorted — just enough for the top to always be the biggest. Contrast this with Binary Heap (the full structure) and Priority Queue (its most common use).


6. Big-O — what "" even means

The parent's headline is . Three symbols to unpack.

Why and not something else here? Because the work per number is one walk down the tree, and the tree's depth is . The tool matches the shape: trees halve, logs count halvings. For how this compares to Merge Sort, Quicksort, and Introsort, see the parent.

Figure — Heap sort — in-place, O(n log n)
Recall What is

? ::: halve , that's four halvings.


7. Swap and "in-place"

This is why the whole page kept insisting on "no pointers, index arithmetic": every trick was chosen so the sort could run inside one row of boxes.


Prerequisite map

Array with 0-based index

Complete binary tree

Binary tree parent and child

Index arithmetic 2i+1 2i+2 floor

Max-heap heap property

Swap in place

Heap sort

Big-O n log n

log = counting halvings

Read it bottom-of-arrow to top: arrays and trees feed the complete tree, which unlocks the index formulas, which power the max-heap; that plus swapping and the Big-O vocabulary is exactly what the parent note assumes you already own.


Equipment checklist

Given , what is ?
— the first box, counting from zero.
Left child index of node ?
.
Right child index of node ?
.
Parent index of node ?
(integer division, round down).
What does equal?
— floor rounds down.
What single rule defines a max-heap?
Every parent is both its children.
Where is the maximum in a max-heap?
At the root, index .
What does "complete" mean for a binary tree?
Every level full top-to-bottom, last level fills left-to-right with no gaps.
What does count?
How many times you can halve before reaching .
What does "in-place / space" mean?
Uses a constant amount of extra memory regardless of .
Does a heap fully sort its elements?
No — only guarantees parent children; the root is the max, but the rest is loosely ordered.