Heapsort

In computer science, heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like selection sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heapsort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step.
Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case runtime. Heapsort is an in-place algorithm, but it is not a stable sort.
Heapsort was invented by J. W. J. Williams in 1964. This was also the birth of the heap, presented already by Williams as a useful data structure in its own right. In the same year, R. W. Floyd published an improved version that could sort an array in-place, continuing his earlier research into the treesort algorithm.
Overview.
The heapsort algorithm can be divided into two parts.
In the first step, a heap is built out of the data (see ). The heap is often placed in an array with the layout of a complete binary tree. The complete binary tree maps the binary tree structure into the array indices; each array index represents a node; the index of the node's parent, left child branch, or right child branch are simple expressions. For a zero-based array, the root node is stored at index 0; if codice_1 is the index of the current node, then
In the second step, a sorted array is created by repeatedly removing the largest element from the heap (the root of the heap), and inserting it into the array. The heap is updated after each removal to maintain the heap property. Once all objects have been removed from the heap, the result is a sorted array.
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 diagrammed here. The heap's invariant is preserved after each extraction, so the only cost is that of extraction.
Algorithm.
The Heapsort algorithm involves preparing the list by first turning it into a max heap. The algorithm then repeatedly swaps the first value of the list with the last value, decreasing the range of values considered in the heap operation by one, and sifting the new first value into its position in the heap. This repeats until the range of considered values is one value in length.
The steps are:
The buildMaxHeap() operation is run once, and is in performance. The siftDown() function is , and is called times. Therefore, the performance of this algorithm is .
Pseudocode.
The following is a simple way to implement the algorithm in pseudocode. Arrays are zero-based and codice_2 is used to exchange two elements of the array. Movement 'down' means from the root towards the leaves, or from lower indices to higher. Note that during the sort, the largest element is at the root of the heap at codice_3, while at the end of the sort, the largest element is in codice_4.
 procedure heapsort(a, count) is
 input: an unordered array "a" of length "count"
 "(Build the heap in array a so that largest value is at the root)"
 heapify(a, count)
 "(The following loop maintains the invariants that a[0:end] is a heap and every element"
 "beyond end is greater than everything before it (so a[end:count] is in sorted order))"
 end ← count - 1
 while end > 0 do
 "(a[0] is the root and largest value. The swap moves it in front of the sorted elements.)"
 swap(a[end], a[0])
 "(the heap size is reduced by one)"
 end ← end - 1
 "(the swap ruined the heap property, so restore it)"
 siftDown(a, 0, end)
The sorting routine uses two subroutines, codice_5 and codice_6. The former is the common in-place heap construction routine, while the latter is a common subroutine for implementing codice_5.
 "(Put elements of 'a' in heap order, in-place)"
 procedure heapify(a, count) is
 "(start is assigned the index in 'a' of the last parent node)"
 "(the last element in a 0-based array is at index count-1; find the parent of that element)"
 start ← iParent(count-1)
 while start ≥ 0 do
 "(sift down the node at index 'start' to the proper place such that all nodes below"
 " the start index are in heap order)"
 siftDown(a, start, count - 1)
 "(go to the next parent node)"
 start ← start - 1
 "(after sifting down the root all nodes/elements are in heap order)"
 "(Repair the heap whose root element is at index 'start', assuming the heaps rooted at its children are valid)"
 procedure siftDown(a, start, end) is
 root ← start
 while iLeftChild(root) ≤ end do "(While the root has at least one child)"
 child ← iLeftChild(root) "(Left child of root)"
 swap ← root "(Keeps track of child to swap with)"
 if a[swap] < a[child] then
 swap ← child
 "(If there is a right child and that child is greater)"
 if child+1 ≤ end and a[swap] < a[child+1] then
 swap ← child + 1
 if swap = root then
 "(The root holds the largest element. Since we assume the heaps rooted at the"
 " children are valid, this means that we are done.)"
 return
 else
 swap(a[root], a[swap])
 root ← swap "(repeat to continue sifting down the child now)"
The codice_5 procedure can be thought of as building a heap from the bottom up by successively sifting downward to establish the heap property. An alternative version (shown below) that builds the heap top-down and sifts upward may be simpler to understand. This codice_9 version can be visualized as starting with an empty heap and successively inserting elements, whereas the codice_6 version given above treats the entire input array as a full but "broken" heap and "repairs" it starting from the last non-trivial sub-heap (that is, the last parent node).
Also, the codice_6 version of heapify has time complexity, while the codice_9 version given below has time complexity due to its equivalence with inserting each element, one at a time, into an empty heap.
This may seem counter-intuitive since, at a glance, it is apparent that the former only makes half as many calls to its logarithmic-time sifting function as the latter; i.e., they seem to differ only by a constant factor, which never affects asymptotic analysis.
To grasp the intuition behind this difference in complexity, note that the number of swaps that may occur during any one siftUp call "increases" with the depth of the node on which the call is made. The crux is that there are many (exponentially many) more "deep" nodes than there are "shallow" nodes in a heap, so that siftUp may have its full logarithmic running-time on the approximately linear number of calls made on the nodes at or near the "bottom" of the heap. On the other hand, the number of swaps that may occur during any one siftDown call "decreases" as the depth of the node on which the call is made increases. Thus, when the codice_6 codice_5 begins and is calling codice_6 on the bottom and most numerous node-layers, each sifting call will incur, at most, a number of swaps equal to the "height" (from the bottom of the heap) of the node on which the sifting call is made. In other words, about half the calls to siftDown will have at most only one swap, then about a quarter of the calls will have at most two swaps, etc.
The heapsort algorithm itself has time complexity using either version of heapify. 
 procedure heapify(a,count) is
 "(end is assigned the index of the first (left) child of the root)"
 end := 1
 while end < count
 "(sift up the node at index end to the proper place such that all nodes above"
 " the end index are in heap order)"
 siftUp(a, 0, end)
 end := end + 1
 "(after sifting up the last node all nodes are in heap order)"
 procedure siftUp(a, start, end) is
 input: "start represents the limit of how far up the heap to sift."
 "end is the node to sift up."
 child := end 
 while child > start
 parent := iParent(child)
 if a[parent] < a[child] then "(out of max-heap order)"
 swap(a[parent], a[child])
 child := parent "(repeat to continue sifting up the parent now)"
 else
 return
Note that unlike codice_6 approach where, after each swap, you need to call only the codice_6 subroutine to fix the broken heap; the codice_9 subroutine alone cannot fix the broken heap. The heap needs to be built every time after a swap by calling the codice_5 procedure since "siftUp" assumes that the element getting swapped ends up in its final place, as opposed to "siftDown" allows for continuous adjustments of items lower in the heap until the invariant is satisfied. The adjusted pseudocode for using codice_9 approach is given below.
 procedure heapsort(a, count) is
 input: an unordered array "a" of length "count"
 "(Build the heap in array a so that largest value is at the root)"
 heapify(a, count)
 "(The following loop maintains the invariants that a[0:end] is a heap and every element"
 "beyond end is greater than everything before it (so a[end:count] is in sorted order))"
 end ← count - 1
 while end > 0 do
 "(a[0] is the root and largest value. The swap moves it in front of the sorted elements.)"
 swap(a[end], a[0])
 "(rebuild the heap using siftUp after the swap ruins the heap property)"
 heapify(a, end)
 "(reduce the heap size by one)"
 end ← end - 1
Variations.
Floyd's heap construction.
The most important variation to the basic algorithm, which is included in all practical implementations, is a heap-construction algorithm by Floyd which runs in time and uses siftdown rather than siftup, avoiding the need to implement siftup at all.
Rather than starting with a trivial heap and repeatedly adding leaves, Floyd's algorithm starts with the leaves, observing that they are trivial but valid heaps by themselves, and then adds parents. Starting with element and working backwards, each internal node is made the root of a valid heap by sifting down. The last step is sifting down the first element, after which the entire array obeys the heap property.
The worst-case number of comparisons during the Floyd's heap-construction phase of Heapsort is known to be equal to , where is the number of 1 bits in the binary representation of and is number of trailing 0 bits.
The standard implementation of Floyd's heap-construction algorithm causes a large number of cache misses once the size of the data exceeds that of the CPU cache. Much better performance on large data sets can be obtained by merging in depth-first order, combining subheaps as soon as possible, rather than combining all subheaps on one level before proceeding to the one above.
Bottom-up heapsort.
Bottom-up heapsort is a variant which reduces the number of comparisons required by a significant factor. While ordinary heapsort requires comparisons worst-case and on average, the bottom-up variant requires comparisons on average, and in the worst case.
If comparisons are cheap (e.g. integer keys) then the difference is unimportant, as top-down heapsort compares values that have already been loaded from memory. If, however, comparisons require a function call or other complex logic, then bottom-up heapsort is advantageous.
This is accomplished by improving the codice_6 procedure. The change improves the linear-time heap-building phase somewhat, but is more significant in the second phase. Like ordinary heapsort, each iteration of the second phase extracts the top of the heap, , and fills the gap it leaves with , then sifts this latter element down the heap. But this element comes from the lowest level of the heap, meaning it is one of the smallest elements in the heap, so the sift-down will likely take many steps to move it back down. In ordinary heapsort, each step of the sift-down requires two comparisons, to find the minimum of three elements: the new node and its two children.
Bottom-up heapsort instead finds the path of largest children to the leaf level of the tree (as if it were inserting −∞) using only one comparison per level. Put another way, it finds a leaf which has the property that it and all of its ancestors are greater than or equal to their siblings. (In the absence of equal keys, this leaf is unique.) Then, from this leaf, it searches "upward" (using one comparison per level) for the correct position in that path to insert . This is the same location as ordinary heapsort finds, and requires the same number of exchanges to perform the insert, but fewer comparisons are required to find that location.
Because it goes all the way to the bottom and then comes back up, it is called heapsort with bounce by some authors.
 function leafSearch(a, i, end) is
 j ← i
 while iRightChild(j) ≤ end do
 "(Determine which of j's two children is the greater)"
 if a[iRightChild(j)] > a[iLeftChild(j)] then
 j ← iRightChild(j)
 else
 j ← iLeftChild(j)
 "(At the last level, there might be only one child)"
 if iLeftChild(j) ≤ end then
 j ← iLeftChild(j)
 return j
The return value of the codice_22 is used in the modified codice_6 routine:
 procedure siftDown(a, i, end) is
 j ← leafSearch(a, i, end)
 while a[i] > a[j] do
 j ← iParent(j)
 x ← a[j]
 a[j] ← a[i]
 while j > i do
 swap x, a[iParent(j)]
 j ← iParent(j)
Bottom-up heapsort was announced as beating quicksort (with median-of-three pivot selection) on arrays of size ≥16000.
A 2008 re-evaluation of this algorithm showed it to be no faster than ordinary heapsort for integer keys, presumably because modern branch prediction nullifies the cost of the predictable comparisons which bottom-up heapsort manages to avoid.
A further refinement does a binary search in the path to the selected leaf, and sorts in a worst case of comparisons, approaching the information-theoretic lower bound of comparisons.
A variant which uses two extra bits per internal node ("n"−1 bits total for an "n"-element heap) to cache information about which child is greater (two bits are required to store three cases: left, right, and unknown) uses less than compares.
Comparison with other sorts.
Heapsort primarily competes with quicksort, another very efficient general purpose in-place comparison-based sort algorithm.
Heapsort's primary advantages are its simple, non-recursive code, minimal auxiliary storage requirement, and reliably good performance: its best and worst cases are within a small constant factor of each other, and of the theoretical lower bound on comparison sorts. While it cannot do better than for pre-sorted inputs, it does not suffer from quicksort's worst case, either. (The latter can be avoided with careful implementation, but that makes quicksort far more complex, and one of the most popular solutions, introsort, uses heapsort for the purpose.)
Its primary disadvantages are its poor locality of reference and its inherently serial nature; the accesses to the implicit tree are widely scattered and mostly random, and there is no straightforward way to convert it to a parallel algorithm.
This makes it popular in embedded systems, real-time computing, and systems concerned with maliciously chosen inputs, such as the Linux kernel. It is also a good choice for any application which does not expect to be bottlenecked on sorting.
A well-implemented quicksort is usually 2–3 times faster than heapsort. Although quicksort requires fewer comparisons, this is a minor factor. (Results claiming twice as many comparisons are measuring the top-down version; see .) The main advantage of quicksort is its much better locality of reference: partitioning is a linear scan with good spatial locality, and the recursive subdivision has good temporal locality. With additional effort, quicksort can also be implemented in mostly branch-free code, and multiple CPUs can be used to sort subpartitions in parallel. Thus, quicksort is preferred when the additional performance justifies the implementation effort.
The other major sorting algorithm is merge sort, but that rarely competes directly with heapsort because it is not in-place. Merge sort's requirement for extra space (roughly half the size of the input) is usually prohibitive except in the situations where merge sort has a clear advantage:
Example.
Let { 6, 5, 3, 1, 8, 7, 2, 4 } be the list that we want to sort from the smallest to the largest. (NOTE, for 'Building the Heap' step: Larger nodes don't stay below smaller node parents. They are swapped with parents, and then recursively checked if another swap is needed, to keep larger numbers above smaller numbers on the heap binary tree.)