// Min_heapify.cpp: defines the entry point for the console application.
//
# Include "stdafx. H"
# Include <iostream>
# Include <algorithm>
Class min_heapify ...{
PRIVATE:
Int m_size;
Int parent (int I)... {return I/2 ;}
Int left (int I)... {return 2 * I ;}
Int right (int I)... {return 2 * I + 1 ;}
Void min_adus_heapify (int A [], int I)
...{
Int L = left (I );
Int r = right (I );
Int lest = I;
If (L <= m_size) & (A [l] <A [I]) //
Lest = L;
// Find the smallest element from elements a [left (I)], a [Right (I)], and a [I.
If (r <= m_size) & (A [R] <A [lest])
Lest = R;
// Store the subscript in largest. If a [I] is the smallest, I and Its Subtrees satisfy the nature of the heap with I as the root subtree.
If (lest! = I)
...{
STD: swap (A [I], a [lest]); //
Min_adus_heapify (A, largest );//
}
// The value of a node whose subscript is largest after the switch is a [I]. The subtree with this node as the root may violate the maximum heap nature. Therefore, recursive call is required.
}
Void build_min_heapify (int A [])
...{
For (INT I = m_size/2; I> = 0; I --)
Min_adus_heapify (A, I );
}
// Heap creation: the elements in the sub-array a [n/2 + 1... n] are leaf nodes, so each of them can be considered as a heap with only one element.
// M_size/2 is the same as m_size, that is, the explanation of the above sentence.
Public:
Min_heapify (): m_size (null )...{}
Min_heapify (int A [], int size)
...{
M_size = size-1;
Build_min_heapify ();
}
Void heap_sort (int A [])
...{
Build_min_heapify ();
For (INT I = m_size; I> = 1; I --)
...{
STD: swap (A [0], a [I]);
-- M_size;
Min_adus_heapify (A, 0 );
}
}
// Build_max_heapify (), which will bring a [0] to the minimum value. In this way, the loop switching is stored in array a [I] And called recursively.
~ Min_heapify ()...{}
};
Int main (INT argc, char * argv [])
...{
Int A [11] =... {30, 45, 22, 72, 9, 12, 26, 80, 22 };
Int size = sizeof (a)/sizeof (A [0]);
Min_heapify P (A, size );
P. heap_sort ();
STD: Copy (A, A + 11, STD: ostream_iterator <int> (STD: cout ,""));
Return 0;
}
/** // * Int main (INT argc, char * argv [])
{
Return 0;
}
*/