The primary advantage of the heap sort is its efficiency. The execution time efficiency of the heapsort is O(n log n). The memory efficiency of the heap sort, unlike the other n log n sorts, is constant, O(1), because the heapsort algorithm is not recursive.

The heap sort algorithm has two major steps. The first major step involves transforming the almost complete tree into a heap. The second major step is to perform the actual sort by extracting the largest element from the root and transforming the remaining tree into a heap.

In this step, the almost complete tree is transformed into a heap. This process is accomplished by beginning at the bottom of the tree. Each leaf node is already a heap, so this process begins with the right-most node on the lowest level that is not a leaf. This subtree is transformed into a heap. When you watch the animation, notice that the nodes at the bottom of the tree turn from blue to red. As each subtree is transformed into a heap, the node that constitutes its root becomes red. This process is done from bottom to top because at each step, the subtree involved is almost a heap--meaning both its left and right subtrees are heaps. So, it is only necessary to correctly reposition the value in the root of the subtree, by moving down the maximum branch and swapping values until it is properly repositioned. The big-O of this transformation into a heap is O(n log n) because it is done on half the nodes (n/2), the reheaping process must travel down one branch of the tree (log n).

Once the tree has been transformed into a heap, the root node contains the largest value--a property that is true of all heaps. The root value is swapped with the value in the right-most node on the lowest level. As you watch the animation, notice that the later node turns back to blue. Also its value in the list at the bottom of the screen becomes blue. The blue nodes and values are those that are in sorted order. Excluding the right-most node at the bottom level from the tree, the remaining subtree is almost a heap. The same process is used to transform it into a heap that was used to turn the original tree into a heap. The big-O of the second step is O(n log n) because it must be performed on each node (n), the reheaping process being log n.