#include<stdio.h> const int INF = 0x7fffffff; void exchange(int &a,int &b) { int tmp; tmp = a; a = b; b = tmp; } void heapFrist(int *array,int k,int len){ if(array == NULL || k <=0 || len <= 0) return; int left,right; left = k*2; right = k*2+1; if(left<=len){ if(array[k]>array[left]){ exchange(array[k],array[left]); } heapFrist(array,left,len); } if(right<=len){ if(array[k]>array[right]){ exchange(array[k],array[right]); } heapFrist(array,right,len); } int fath = k>>1; if(fath>0&&array[fath]>array[k]){ exchange(array[k],array[fath]); if(left<=len&&array[k]>array[left]){ exchange(array[k],array[left]); } if(right<=len&&array[k]>array[right]){ exchange(array[k],array[right]); } } } void print(int *array,int k,int len) { printf("%d ",array[k]); if(k*2<=len) print(array,k*2,len); if(k*2+1<=len) print(array,k*2+1,len); } void heapScend(int *array,int k,int len) { if(array == NULL || k <=0 || len <= 0) return; int left,right; left = k<<1; right = k<<1|1; if(right<=len){ if(array[right]>array[left]){ exchange(array[left],array[k]); heapScend(array,left,len); } else{ exchange(array[right],array[k]); heapScend(array,right,len); } } else if(left<=len){ exchange(array[left],array[k]); heapScend(array,left,len); } } int main() { int len,array[100]; while(scanf("%d",&len)>0) { for(int i=1; i<=len;i++) { scanf("%d",&array[i]); } heapFrist(array,1,len); //print(array,1,len); for(int i = 1; i<=len; i++) { printf("%d ",array[1]); array[1] = INF; heapScend(array,1,len); } printf("\n"); } }