Heapsort

Heapsort
A run of heapsort sorting an array of randomly permuted values. In the first stage of the algorithm the array elements are reordered to satisfy the heap property. Before the actual sorting takes place, the heap tree structure is shown briefly for illustration.
ClassSorting algorithm
Data structureArray
Worst-case performance
Best-case performance (distinct keys)[1][2]
or (equal keys)
Average performance
Worst-case space complexity total auxiliary

In computer science, heapsort is an efficient, comparison-based sorting algorithm that reorganizes an input array into a heap (a data structure where each node is greater than its children) and then repeatedly removes the largest node from that heap, placing it at the end of the array.[3]

Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantages of very simple implementation and a more favorable worst-case O(n log n) runtime. Most real-world quicksort variants include an implementation of heapsort as a fallback should they detect that quicksort is becoming degenerate. Heapsort is an in-place algorithm, but it is not a stable sort.

Heapsort was invented by J. W. J. Williams in 1964.[4] The paper also introduced the binary heap as a useful data structure in its own right.[5] In the same year, Robert W. Floyd published an improved version that could sort an array in-place, continuing his earlier research into the treesort algorithm.[5]

  1. ^ Bollobás, B.; Fenner, T. I.; Frieze, A. M. (1996). "On the Best Case of Heapsort" (PDF). Journal of Algorithms. 20 (11): 205–217. doi:10.1006/jagm.1996.0011.
  2. ^ Sedgewick, Robert; Schaffer, Russel W. (October 1990). The Best Case of Heapsort (Technical report). Princeton University. TR-293-90.
  3. ^ Cormen, Thomas H.; Leiserson, Charles Eric; Rivest, Ronald L.; Stein, Clifford (2022). Introduction to algorithms (4th ed.). Cambridge, Massachusetts: The MIT Press. p. 170. ISBN 978-0-262-04630-5.
  4. ^ Williams 1964
  5. ^ a b Brass, Peter (2008). Advanced Data Structures. Cambridge University Press. p. 209. ISBN 978-0-521-88037-4.

From Wikipedia, the free encyclopedia · View on Wikipedia

Developed by Nelliwinne