Content deleted Content added
→Algorithm: fix <syntaxhighlight> error |
Link suggestions feature: 3 links added. |
||
(8 intermediate revisions by 4 users not shown) | |||
Line 1:
{{Short description|Comparison-based sorting algorithm}}
In [[computer science]], '''adaptive heap sort''' is a [[Comparison sort|comparison-based]] [[sorting algorithm]] of the [[Adaptive sort|adaptive sort family]]. It is a variant of [[Heapsort|heap sort]] that performs better when the data contains existing order. Published by [[Christos Levcopoulos]] and [[Ola Petersson]] in 1992, the algorithm utilizes a new measure of presortedness, ''Osc,'' as the number of oscillations.<ref name=":0">{{Cite journal|last1=Levcopoulos|first1=C.|last2=Petersson|first2=O.|date=1993-05-01|title=Adaptive Heapsort|journal=Journal of Algorithms|volume=14|issue=3|pages=395–413|doi=10.1006/jagm.1993.1021|issn=0196-6774}}</ref> Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high.<ref name=":0" />
== Heapsort ==
Heap sort is a sorting algorithm that utilizes [[binary heap]] [[data structure]]. The method treats an array as a complete [[binary tree]] and builds up a Max-Heap/Min-Heap to achieve sorting.<ref name=":1">{{Cite journal|last1=Schaffer|first1=R.|last2=Sedgewick|first2=R.|date=1993-07-01|title=The Analysis of Heapsort|journal=Journal of Algorithms|volume=15|issue=1|pages=76–100|doi=10.1006/jagm.1993.1031|issn=0196-6774}}</ref> It usually involves the following four steps.
# Build a Max-Heap(Min-Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for ''Min-Heap'') to each of its child nodes.
Line 9 ⟶ 10:
# Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted.
Below is a C/C++ implementation that builds up a Max-Heap and sorts the array after the heap is built.
<syntaxhighlight lang="c
/*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
void heapify(int array[], int start, int end); // A function that build up a max-heap binary tree▼
void heapify(int array[], int start, int end)
{
Line 25 ⟶ 23:
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end
{
if (array[child + 1] > array[child])
Line 47 ⟶ 45:
}
void heap_sort (int array[], int len)
{
Line 61 ⟶ 58:
}
}
int main()
{
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1}; //the array that will be sorted▼
//the array that will be sorted
▲ int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array_len = sizeof(array)/sizeof(*array); //length of the array
heap_sort (array, array_len);
return 0;
}
Line 74 ⟶ 77:
=== Oscillations (''Osc'') ===
For sequence <math>X =
=== Other measures ===
Besides the original Osc measurement, other known measures include the number of inversions ''Inv'', the number of runs ''Runs'', the number of blocks ''Block'', and the measures ''Max'', ''Exc'' and ''Rem''. Most of these different measurements are related for adaptive heap sort. Some measures dominate the others: every Osc-optimal algorithm is Inv optimal and Runs optimal; every Inv-optimal algorithm is Max optimal; and every Block-optimal algorithm is Exc optimal and Rem optimal.<ref name=":2">{{Cite
== Algorithm ==
Adaptive heap sort is a variant of heap sort that seeks optimality ([[Asymptotically optimal algorithm|asymptotically optimal]]) with respect to the lower bound derived with the measure of presortedness by taking advantage of the existing order in the data. In heap sort, for a data <math>X =
First, a [[Cartesian tree]] is built from the input in <math>O(n)</math> time by putting the data into a binary tree and making each node in the tree is greater(or smaller) than all its children nodes, and the root of the Cartesian tree is inserted into an empty binary heap. Then repeatedly extract the maximum from the binary heap, retrieve the maximum in the Cartesian tree, and add its left and right children (if any) which are themselves Cartesian trees, to the binary heap. If the input is already nearly sorted, the Cartesian trees will be very unbalanced, with few nodes having left and right children, resulting in the binary heap remaining small, and allowing the algorithm to sort more quickly than <math>O(n\log n)</math> for inputs that are already nearly sorted.<ref>{{Cite web|url=http://www.keithschwarz.com/interesting/code/?dir=cartesian-tree-sort|title=Archive of Interesting Code|website=www.keithschwarz.com|access-date=2019-10-31}}</ref>
Line 113 ⟶ 116:
{{reflist}}
[[Category:Comparison sorts]]
[[Category:Heaps (data structures)]]
|