#include<iostream> #include<vector> using namespace std; void maxHeep(vector<int> &list, int begin, int end){ int dad = begin; int son = dad * 2 + 1; while (son <= end){ if ((son + 1 <= end)&&(list[son]<list[son + 1])) son++; if (list[dad] > list[son]) return; else{ int temp; temp = list[dad]; list[dad] = list[son]; list[son] = temp; dad = son; son = dad * 2 + 1; } } } void heepSort(vector<int> &list){ int len = list.size(); for (int i = len / 2 - 1; i >= 0; --i){ maxHeep(list, i,len-1); } for (int i = len - 1; i >0; --i){ int temp = list[i]; list[i] = list[0]; list[0] = temp; maxHeep(list, 0, i-1); } } int main() { vector<int> list = { 9, 3, 2, 5, 3, 3, 2, 0, -1, 3, 2, 5, 3, 3, 2, 0, -1, 3, 2, 5, 3, 3, 2, 0, -1,999 }; heepSort(list); for (int i = 0; i < list.size(); ++i){ cout << list[i] << ","; } }