Overview

In the second step, a sorted array is created by repeatedly removing the largest element from the heap, and inserting it into the array. The heap is reconstructed after each removal. Once all objects have been removed from the heap, we have a sorted array. The direction of the sorted elements can be varied by choosing a min-heap or max-heap in step one.

Heapsort can be performed in place. The array can be split into two parts, the sorted array and the heap. The storage of heaps as arrays is diagrammedhere. The heap's invariant is preserved after each extraction, so the only cost is that of extraction.

Variations

The most important variation to the simple variant is an improvement byR. W. Floydthat, in practice, gives about a 25% speed improvement by using only one comparison in eachsiftuprun, which must be followed by asiftdownfor the original child. Moreover, it is more elegant to formulate. Heapsort's natural way of indexing works on indices from 1 up to the number of items. Therefore the start address of the data should be shifted such that this logic can be implemented avoiding unnecessary +/- 1 offsets in the coded algorithm. The worst-case number of comparisons during the Floyd's heap-construction phase of Heapsort is known to be equal to 2N − 2s2(N) − e2(N), where s2(N) is the sum of all digits of the binary representation of N and e2(N) is the exponent of 2 in the prime factorization of N.

Ternary heapsortuses a ternary heap instead of a binary heap; that is, each element in the heap has three children. It is more complicated to program, but does a constant number of times fewer swap and comparison operations. This is because each step in the shift operation of a ternary heap requires three comparisons and one swap, whereas in a binary heap two comparisons and one swap are required. The ternary heap does two steps in less time than the binary heap requires for three steps, which multiplies the index by a factor of 9 instead of the factor 8 of three binary steps. Ternary heapsort is about 12% faster than the simple variant of binary heapsort.

Thesmoothsortalgorithmis a variation of heapsort developed byEdsger Dijkstrain 1981. Like heapsort, smoothsort's upper bound isO(nlog n). The advantage of smoothsort is that it comes closer to O(n) time if theinput is already sorted to some degree, whereas heapsort averages O(nlog n) regardless of the initial sorted state. Due to its complexity, smoothsort is rarely used.

Levcopoulos and Peterssondescribe a variation of heapsort based on aCartesian treethat does not add an element to the heap until smaller values on both sides of it have already been included in the sorted output. As they show, this modification can allow the algorithm to sort more quickly than O(n log n) for inputs that are already nearly sorted.

Comparison with other sorts

Quicksort is typically somewhat faster due to some factors, but the worst-case running time for quicksort isO(n2), which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk. Seequicksortfor a detailed discussion of this problem and possible solutions.

Thus, because of the O(nlogn) upper bound on heapsort's running time and constant upper bound on its auxiliary storage, embedded systems with real-time constraints or systems concerned with security often use heapsort.

Heapsort also competes withmerge sort, which has the same time bounds. Merge sort requires Ω(n) auxiliary space, but heapsort requires only a constant amount. Heapsort typically runs faster in practice on machines with small or slowdata caches. On the other hand, merge sort has several advantages over heapsort: