The
is implemented as follows:
int getparent (int c);
int getLeft (int p);
int GetRight (int p);
void swap (int *p1, int *p2);
void Heap_sort (int *source, int length);
void max_heapify (int *source, int length, int loc);
void Build_max_heap (int *source, int length);
void heap_sort_iteration (int *source, int length);
void max_heapify_iteration (int *source, int length, int loc);
void build_max_heap_iteration (int *source, int length); int getparent (int c) {return (c-1)/2.} int getLeft (int p) {return p * 2 + 1;} int getright (int p) {RE
Turn P * 2 + 2;
} void Swap (int *p1, int *p2) {int temp = *P1;
*P1 = *P2;
*P2 = temp;
} void max_heapify (int *source, int length, int loc) {int l = getLeft (loc);
int r = GetRight (loc);
int max = loc;
if (l < length && source[l] > Source[max]) max = l;
if (R < length && Source[r] > Source[max]) max = R;
if (max = = loc) return;
Swap (source + max, source + loc); Max_heapify (SOurce, length, max); } void Build_max_heap (int *source, int length) {for (int i = GetParent (length-1); I >= 0; i.) {Max_hea
Pify (source, length, i);
} void Heap_sort (int *source, int length) {build_max_heap (source, length);
for (int i = length-1 i > 0; i.) {swap (source, source + i);
Max_heapify (source, I, 0);
} void max_heapify_iteration (int *source, int length, int loc) {int L, R;
int max = loc;
while (Loc < length) {L = getLeft (loc);
r = GetRight (loc);
if (l < length && source[l] > Source[max]) max = l;
if (R < length && Source[r] > Source[max]) max = R;
if (max = = loc) return;
Swap (source + max, source + loc);
LOC = max;
} void build_max_heap_iteration (int *source, int length) {for (int i = GetParent (length-1); I >= 0; i) { Max_heapify_iteration (Source, Length, I);
} void heap_sort_iteration (int *source, int length) {build_max_heap (source, length);
for (int i = length-1 i > 0; i.) {swap (source, source + i);
Max_heapify_iteration (source, I, 0); }
}
Heap sorting is the sort of unstable in-situ. The
array used in this program acts as a data structure for the heap, which computes the coordinates of the left child, right child, and parent node as shown in Parent,left_child,right_child. The
Max_heapify keeps the property of the maximum heap, assuming that both the P node and the left two subtrees are the largest heap, max_heapify the P node to maintain the maximum heap properties, select the largest node in the right and left of the P node to exchange with P nodes, and then proceed recursively to the switched nodes Max_ Heapify operation until the leaf node is reached. The asymptotic upper bound of max_heapify operation time is 0 (h) 0 (H) H as the height of the node to be operated. The
Build_max_heap sets an array to the maximum heap, calling the Max_heapify function separately from the last non-leaf node to the root node. The asymptotic bounds of its operating time are 0 (n) 0 (n). The
Heap_sort function sorts the number of n, each time the first element and the last element of the maximum heap is exchanged, the largest element is obtained, the number of elements in the heap is reduced, then the root node of the heap is max_heapify, and the largest element in the current heap is obtained. You can sort the array by looping n-1 times. Its operating time is asymptotically accurate bounded by 0 (nlogn) 0 (NLOGN).
All algorithmic complexity calculations on its algorithm are presented in chapter sixth of the algorithm, involving the main theorem no longer repeat.