Heapsort - Comparison With Other Sorts

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(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. Merge sort requires Ω(n) auxiliary space, but heapsort requires only a constant amount. Heapsort typically runs faster in practice on machines with small or slow data caches. On the other hand, merge sort has several advantages over heapsort:

  • Like quicksort, merge sort on arrays has considerably better data cache performance, often outperforming heapsort on modern desktop computers because merge sort frequently accesses contiguous memory locations (good locality of reference); heapsort references are spread throughout the heap.
  • Heapsort is not a stable sort; merge sort is stable.
  • Merge sort parallelizes well and can achieve close to linear speedup with a trivial implementation; heapsort is not an obvious candidate for a parallel algorithm.
  • Merge sort can be adapted to operate on linked lists with O(1) extra space. Heapsort can be adapted to operate on doubly linked lists with only O(1) extra space overhead.
  • Merge sort is used in external sorting; heapsort is not. Locality of reference is the issue.

Introsort is an interesting alternative to heapsort that combines quicksort and heapsort to retain advantages of both: worst case speed of heapsort and average speed of quicksort.

Read more about this topic:  Heapsort

Famous quotes containing the words comparison with, comparison and/or sorts:

    He was a superior man. He did not value his bodily life in comparison with ideal things. He did not recognize unjust human laws, but resisted them as he was bid. For once we are lifted out of the trivialness and dust of politics into the region of truth and manhood.
    Henry David Thoreau (1817–1862)

    What is man in nature? A nothing in comparison with the infinite, an all in comparison with the nothing—a mean between nothing and everything.
    Blaise Pascal (1623–1662)

    It’s all sorts of middle-aged white men in suits—forests of middle-aged men in dark suits. All slightly red-faced from eating and drinking too much.
    Diane Abbott (b. 1953)