Information AboutHeapsort |
| CATEGORIES ABOUT HEAPSORT | |
| sorting algorithms | |
| comparison sorts | |
| heaps structure | |
| articles with example pseudocode | |
| articles with example c code | |
|
OVERVIEW One simple way to sort a list of objects is to use a Heap data structure. We add all of our objects into the heap, and the heap organizes the elements added to it in such a way that we can quickly extract either the largest value (in a max-heap) or the smallest value (in a min-heap). Moreover, because this operation preserves the heap's structure, we can extract the largest/smallest value over and over again until none remain. This gives us the elements in order. 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 .) 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: VARIATIONS Although not widely known, it is possible to define a ternary heapsort which uses a ternary heap instead of a binary heap; that is, each element in the heap has three children. Ternary heapsort is somewhat more complicated to program, but it is potentially faster. Each step in the sift 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 can do two steps in less time than the binary heap requires for three steps. But two steps of a ternary tree multiply the index by a factor of 9, which is more than the factor 8 of three binary steps. Ternary heapsort is about 12% faster than binary heapsort. COMPARISON WITH OTHER SORTS Heapsort primarily competes with Quicksort , another very efficient general purpose nearly-in-place comparison-based sort algorithm. Quicksort is typically somewhat faster, due to better cache behavior and other factors, but the worst-case running time for quicksort is O(''n''2), 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 System s, 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 Cache s. On the other hand, merge sort has several advantages over heapsort:
IMPLEMENTATION IN PSEUDOCODE The following is one 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. 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(a a[0 ) sift(a, 0, end) end := end - 1 } function sift(a, start, count) { var ''int'' root := start, child
if child < count - 1 '''and''' a < a[child + 1 child := child + 1 if a < a[child swap(a a[child ) root := child else return } } IMPLEMENTATION IN C This is a fast implementation of heapsort in C, adapted from ''Numerical Recipes in C'' but designed to be slightly more readable and to index from 0. void heapsort('''int''' arr {Link without Title} , '''unsigned int''' N) { unsigned int n = N, i = n/2, parent, child; int t;
}
while (child < n) { if (child + 1 < n && arr + 1 > arr[child]) {
}
} else {
} }
} } REFERENCES
EXTERNAL LINKS
|
|
|