C ++ implements heap sorting tutorial and heap sorting tutorial
This is an example of an introduction to algorithms.
Refer to the following pseudocode
C ++ code
# Include
Using namespace std; // swap two data sizes void exchange (int & a, int & B) {int temp; temp = a; a = B; B = temp ;} // left child, right child int left (int I) {return I * 2;} int right (int I) {return I * 2 + 1 ;} // void maxHeapify (int array [], int len, int I) {int l = left (I), r = right (I ); int largest = I; if (l
Array [largest]) largest = l; if (r
Array [largest]) largest = r; if (largest! = I) {exchange (array [largest], array [I]); maxHeapify (array, len, largest ); // recursive adjustment} // sort void heapSort (int array [], int size) {for (int I = size/2-1; I> = 0; -- I) {maxHeapify (array, size, I) ;}for (int I = size-1; I> = 1; -- I) {exchange (array [0], array [I]); maxHeapify (array, I, 0) ;}} int main () {int Array [10] = }; cout <"original heap:" <
Running result