/* Heap sorting (large top heap) * // after creating a large top heap, adjust it to a small top heap, then output # include <iostream> # include <algorithm> using namespace STD; // adjust the heap // adjust all the child and grandson nodes whose current node is the parent node... Void heapadjust (int * a, int I, int size) {int lchild = 2 * I; // The left child node number of I int rchild = 2 * I + 1; // The right child node number of I int max = I; // Temporary Variable if (I <= size/2) // If I is a leaf node, no adjustment is required. {If (lchild <= size & A [lchild]> A [Max]) {max = lchild ;} if (rchild <= size & A [rchild]> A [Max]) {max = rchild;} If (max! = I) {swap (A [I], a [Max]); heapadjust (A, Max, size ); // avoid having to adjust the child tree with Max as the parent node instead of heap }}// right-to-left, bottom-to-top, and "Triangle" one by one (one parent and two children) /* 1 2 34 5 6 7 * // For example, in the preceding tree, we first adjust the number in the three positions 3, 6, and 7 to make it a big top heap, // adjust the numbers 2, 4, and 5 to make them a big heap, and then adjust all the child nodes and grandson nodes with 1 as the parent node. // The entire tree becomes a big heap. Void buildheap (int * a, int size) {int I; for (I = size/2; I> = 1; I --) // The maximum number of non-leaf nodes is size/2 {heapadjust (A, I, size );}} // heap sorting ---- adjust the previously created Big Top heap to a small top heap. Void heapsort (int * a, int size) {int I; buildheap (A, size); for (I = size; I> = 1; I --) {swap (A [1], a [I]); // swap the heap top and the last element, that is, each time the creator of the remaining element is placed at the end of heapadjust (A, 1, i-1); // re-adjust the heap top node to become a big top heap} int main (INT argc, char * argv []) {int A [100]; int size; while (scanf ("% d", & size) = 1 & size> 0) {int I; cout <"Before sorting:"; for (I = 1; I <= size; I ++) {CIN> A [I];} // buildheap (A, size); heapsort (A, size ); cout <"after sorting:"; for (I = 1; I <= size; I ++) {cout <A [I] <"";} cout <Endl;} return 0 ;}