#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define max_size 100
using namespace std;
void BuidHeap(int *a,int size);
void HeapAdjust(int i,int *a,int size);
void HeapSort(int *a , int size);
void BuidHeap(int *a,int size){
int i;
for (i = size/2;i >= 1;i--){
HeapAdjust(i,a,size);
}
}
void HeapAdjust(int i,int *a,int size){
int lchild = 2 * i;
int rchild = 2 * i + 1;
int max = i;
if (i <= size/2){
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(max,a,size);
}
}
}
void HeapSort(int *a , int size){
int i;
BuidHeap(a,size);
for (i = size; i >= 1 ; i--)
{
swap(a[1],a[i]);
HeapAdjust(1,a,i-1);
}
}
int main(){
int size;
int a[max_size];
while(scanf("%d",&size) == 1&& size>0){
for (int i = 1; i <= size; ++i){
cin>>a[i];
}
HeapSort(a,size);
for (int i = 1; i <= size; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}
网友评论