Heapsort is a comparison-based sorting algorithm with $O(n log n)$ running time.

It sorts in place, using only constant extra space.

Heapsort combines:

It is based on a data structure called a heap, which is also widely used to implement priority queues.


6.1 Heaps

A binary heap is an array that represents a nearly complete binary tree:

The root of the heap is at A[1] or A[0] based on your indexing.


Array Representation

For a node at index i:

These operations are very fast and often implemented inline.