Definition of heap:
N-keyword sequences kl,k2,...,kn called heaps, when and only if the sequence satisfies the following properties (referred to as heap properties):
(1) Ki≤k2i and Ki≤k2i+1 or (2) ki≥k2i and ki≥k2i+1 (1≤i≤)
If the vector stored by this sequence is R[1..N] as a storage structure of a complete binary tree, the heap is essentially a complete binary tree that satisfies the following properties: None of the key words in the tree is greater than (or not less than) the key word for its left and right children (if any).
This property of the heap makes it possible to quickly locate the smallest (large) element in a sequence.
The process of the heap sort algorithm is as follows: 1 the smallest (large) element 2 of the current sequence is exchanged for this element and the last element. So the current smallest (large) element is placed at the end of the sequence, and the exchange of the original last element in the first 3 of the sequence may destroy the nature of the heap sequence ( Note that the sequence at this point is to remove the element that is already on the last side, so the sequence needs to be adjusted so that it satisfies the properties of the heap above. Repeat the above procedure until the sequence is adjusted.
The array is the heap array to be adjusted, I is the position of the array element to be adjusted, length is the size of the array
void Heapadjust (int array[], int i, int nlength)
{
int Nchild, ntemp;
for (ntemp = array[i]; 2 * i + 1 < nlength; i = nchild)
{
The location of the child node is the parent node position * 2 + 1
Nchild = 2 * i + 1;
To get the larger node in the sub node.
if (nchild!= nLength-1 && array[nchild + 1] > Array[nchild])
++nchild;
If the larger child node is larger than the parent node, move the larger child node up and replace its parent node.
if (Ntemp < Array[nchild])
{
Array[i] = Array[nchild];
}
else//Otherwise exit loop
{
Break
}
}
Finally put the element values that need to be adjusted to the right place
Array[i] = ntemp;
}
Heap Sorting algorithm
void heapsort (int array[], int length)
{
Adjusts the first half of the sequence, and after it is adjusted, is the largest element of the sequence.
for (int i = LENGTH/2-1; I >= 0; i.)
{
Heapadjust (array, I, length);
}
Adjusts the sequence from the last element, constantly narrowing the adjustment until the first element
for (int i = length-1 i > 0; i.)
{
Swap the first element with the current last element,
To ensure that the elements of the current last position are the largest in the present sequence.
Swap (&array[0], &array[i]);
Constantly narrowing the range of adjustment heap to ensure that the first element is the maximum value of the current sequence, each time it is adjusted
Heapadjust (array, 0, i);
}
}
The result of a test and output, showing the current array after each heapadjust
before Heap sort:
71 18 151 138 160 63 174 169 79 78
// 开始调整前半段数组元素
71 18 151 138 160 63 174 169 79 78
71 18 151 169 160 63 174 138 79 78
71 18 174 169 160 63 151 138 79 78
71 169 174 138 160 63 151 18 79 78
174 169 151 138 160 63 71 18 79 78
// 开始进行全局的调整
169 160 151 138 78 63 71 18 79 174
160 138 151 79 78 63 71 18 169 174
151 138 71 79 78 63 18 160 169 174
138 79 71 18 78 63 151 160 169 174
79 78 71 18 63 138 151 160 169 174
78 63 71 18 79 138 151 160 169 174
71 63 18 78 79 138 151 160 169 174
63 18 71 78 79 138 151 160 169 174
18 63 71 78 79 138 151 160 169 174
Animation Demo:
Http://202.113.89.254/DataStructure/DS/web/flashhtml/duipaixu.htm