Heap sort C + + implementation #include<iostream> #include <functional>using namespace std;void heapsort (int* arr, int cnt) { Function<void (int*, int, int) > Percdown = [&] (int* arr, int i, int cnt)//down filter operation {int child = 0, TMP = 0;FOR (tmp = Arr[i]; 2 * i + 1 < cnt; i = child) {child = i * 2 + 1;if (child! = cnt-1 && arr[child + 1] > Arr[child]) child++;if (TMP < Arr[child ]) arr[i] = Arr[child];elsebreak;} Arr[i] = tmp;}; for (int i = (cnt-1)/2; I >= 0; i--)//starting from the parent of the last element a one-off filter, building the heap note: i = CNT or cnt-1 here, can be self-drawing interpretation percdown (arr, I, CNT), for (int i = cnt-1; i > 0; i--)//To sort the constructed heap {swap (arr[0], arr[i]);p ercdown (arr, 0, i);}} int main () {const int cnt = 1159;int Arr[cnt];int tmp = -1;for (int i = 0; i < cnt; i++) {if (i% 2) arr[i] = I;elsearr[i] = i * TMP;} Heapsort (arr, CNT); for (auto I:arr) cout << i << endl;for (int i = 0; i < cnt-1; i++) {if (Arr[i] > Arr [i + 1]) cout << "haha" << endl;} cout << "Test End" << endl;cin.Get (); return 0;}
Heap sort C + + implementation