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.
A binary heap is an array that represents a nearly complete binary tree:
A[1 .. heap-size] belong to the heapThe root of the heap is at A[1] or A[0] based on your indexing.
For a node at index i:
PARENT(i) = floor(i / 2) or shift i in binary once to rightLEFT(i) = 2i or shift i in binary once to leftRIGHT(i) = 2i + 1 or shift i in binary once to left and plus 1 bitThese operations are very fast and often implemented inline.