The central task in the heapsort algorithm is restoring the heap after each
removal of the root element. This reheaping takes
*O*(*log*(*n*))
time, for a
total of
*O*(*nlog*(*n*))
time since there are n elements. It seem
counterintuitive that heap sort would be so efficient since building the
heap often increases the number of inversions in the array. In fact it is
not only
*O*(*nlogn*)
in the average case, but it is
*O*(*nlog*(*n*))
in all
cases, unlike quick sort, which is quadratic in the worst case.

To walk through the process again, moving an element up from node 1 takes
on the order of log(n) steps since there are log(n) levels in the tree that
the value may have to move through. Therefore heapsort will take
*O*(*nlog*(*n*))
time, sifting up to
*log*(*n*)
levels for each element sorted. Even though the
heap gets smaller as the array is sorted, it doesn't get smaller very fast.
Half the elements in the initial heap are in the leaves, and after being
exchanged with the root, each of them can be expected to move about
*log*(*n*)
levels back.

Take a Study Break!