堆排序
include iostream> include math.h>using namespace std;
int num[100] = { 0 };
int n;
void swap(int a, int b){
int tmp;
tmp = num[a];
num[a] = num[b];
num[b] = tmp;
}
void heapAdjust(int s, int m){
int rc = num[s];
int j;
for (j = 2 s; j <= m; j = 2){
if (j < m && num[j]<num[j + 1]){
++j;
}
if (rc >= num[j]){
break;
}
num[s] = num[j];
s = j;
}
num[s] = rc;
}
void heapsort(){
int i;
for (i = n / 2; i > 0; --i){
heapAdjust(i, n);
}
for (i = n; i > 1; --i){
swap(1, i);
heapAdjust(1, i - 1);
}
}
int main(){
int d[10] = { 0 };
int i;
while (cin >> n){
for (i = 1; i <=n; i++){
cin >> num[i];
}
heapsort();
for (i = 1; i <= n; i++){
cout << num[i] << " ";
}
cout << endl;
}
return 0;
}