Heapsort is one of the best general-purpose sorting algorithms, a comparison sort and part of the selection sort family. Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantages of worst-case O(n log n) runtime and being an in-place algorithm. Heapsort is not a stable sort.
In doing so, the only extra space required is that needed to store the heap. In order to achieve constant space overhead, we use a trick: we store a binary heap (or alternatively, a heap with more than two children) inside the part of the input array which has not yet been sorted. (The structure of this heap is described at Binary heap: Heap implementation.) Heapsort makes use of two standard heap operations: insertion and root deletion. Each time we delete (extract) the maximum, we place it in the last location of the array not yet occupied, and use the remaining prefix of the array as a heap holding the remaining unsorted elements:
| Heap of remaining unsorted elements | Sorted elements |
Quicksort is typically somewhat faster, due to better cache behavior and other factors, but the worst-case running time for quicksort is O(n2), which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk. See quicksort for a detailed discussion of this problem, and possible solutions.
The quicksort algorithm also requires Ω(log n) extra storage space, making it not a strictly in-place algorithm. This typically does not pose a problem except on the smallest embedded systems, or on systems where memory allocation is highly restricted. Constant space (in-place) variants of quicksort are possible to construct, but are rarely used in practice due to their extra complexity.
Thus, because of the O(n log n) 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 with merge sort, which has the same time bounds, but requires Ω(n) auxiliary space, whereas heapsort requires only a constant amount. Heapsort also typically runs more quickly in practice on machines with small or slow data caches. On the other hand, merge sort has several advantages over heapsort:
The following is the "standard" way to implement the algorithm, in pseudocode, where swap is used to swap two elements of the array. Notice that the arrays are zero-based in this example. The astute reader will notice something odd about this.
function heapSort(a, count) { var int start := count ÷ 2 - 1, end := count - 1 while start ≥ 0 sift(a, start, count) start := start - 1 while end > 0 swap(aa[0) sift(a, 0, end) end := end - 1 } function sift(a, start, count) { var int root := start, child while root * 2 + 1 < count { child := root * 2 + 1 if child < count - 1 and a< a[child + 1 child := child + 1 if a< a[child swap(aa[child) root := child else return } }
The strange thing about the above implementation is that it uses heapify-down operations to achieve what we really want to achieve using heapify-up operations. Imagine building the heap. As we add new elements, we want them to crawl up the heap. For the actual sorting, however, the standard implementation jibes with intuition. The following implementation jibes completely with intuition and is still O(n lg n).
function heapSort(a, count) { var int start := 0, end := count - 1 while start ≤ count - 2 start := start + 1 siftup(a, start) while end > 0 swap(aa[0) siftdown(a, 0, end) end := end - 1 } function siftdown(a, start, count) { var int root := start, child while root * 2 + 1 < count { child := root * 2 + 1 if child < count - 1 and a< a[child + 1 child := child + 1 if a< a[child swap(aa[child) root := child else return } } function siftup(a, start) { var int child := start, root, remainder while child > 0 { remainder := (child - 1) % 2 root := ((child - 1) - remainder) / 2 if a< a[child swap(aa[child) child := root else return } }
Sort algorithms | Comparison sorts
Heapsort | Heapsort | Heapsort | Tri par tas | Krūvos rūšiavimo algoritmas | Heapsort | Heapsort | ヒープソート | Sortowanie przez kopcowanie | Heapsort | 堆排序
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Heapsort".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world