Heap Sort

Heap Sort is variant of a selection sort.
Heap Sort is O(N log N), but it's almost always slower than either Quick Sort or Merge Sort.
It's also more confusing than Quick Sort or Merge Sort, so it's rarely used.
Heap Sort does have one interesting bit, the concept of a heap

What's a heap?
A heap (as used in heap sort) is a binary tree where each node has (at most) two child nodes.

2 3 10 1 5 4 8 6 7 9
The bottom row of the heap is always filled from left to right.
Every node forms a sub-heap -- all the values that descend from that node.
If each parent is not smaller than it's children, then the heap is called "correct".
If a parent is smaller than one of it's children, then the heap is called "incorrect".
If a sub-heap is incorrect, any heap that contains it is also incorrect.
10 3 9 1 5 4 8 6 7 2

(In this example, the incorrect heaps are colored yellow)

There are few things to take note of;

A node without children is correct.
You can remove the last element on the bottom right of a correct heap, and you will still have a correct heap.
Every parent node in a correct heap is bigger than all it's descendants (all values beneath it in the heap).
The top element is always the biggest element in a correct heap.

There are several operations that can be done on a heap.
If you want the full description, here's a link that explains more of what a heap is and some other things about heaps in general.

For Heap Sort, the only operations we need are Sift-Down and Heapify

Sift-Down;
Starting with the parent of two correct child nodes;
Continue until either the parent is already bigger when sifted down (no swap necessary) or you reach the bottom of the heap.

10 3 9 1 5 4 8 6 2 7
10 3 9 1 5 4 8 6 2 7
10 3 9 1 5 4 8 6 2 7
10 3 9 1 5 4 8 6 2 7

For heap sort to work, we need to start with a correct heap.
To create a correct heap, we use the procedure Heapify;

Heapify
Starting with the last parent node and working backwards to the top, call sift down on each node.
Since a child-less node is correct, the last parent node is guaranteed to be atop two correct sub-nodes.
As we work backwards, each node is atop sub-nodes that have already been sifted-down, and are thus correct.
Once every node has been sifted down, the entire heap is correct.

Once we have a correct heap, we sort it by repeating the following until the heap is empty;

A few frequently asked questions;


^ UP ^    < Back <    > Next >