<< Chapter < Page | Chapter >> Page > |
During extraction, the only space required is that needed to store the heap. In order to achieve constant space overhead, the heap is stored in the part of the input array that has not yet been sorted. (The structure of this heap is described at Binary heap: Heap implementation .)
Heapsort uses two heap operations: insertion and root deletion. Each extraction places an element in the last empty location of the array. The remaining prefix of the array stores the unsorted elements.
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 (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.
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:
Notification Switch
Would you like to follow the 'Data structures and algorithms' conversation and receive update notifications?