//heapsort #include<bits/stdc++.h> using namespace std; const int maxn =105; int a[maxn]; int n; void HeapInsert(int a[],int index) { while(a[index] > a[(index-1)/2]) { swap(a[index],a[(index-1)/2]); index = (index - 1) / 2; } } void Heapify(int a[], int index, int size) { int left = index * 2 + 1; while(left < size) { int largest = left + 1 < size && a[left+1] > a[left] ? left + 1 : left; largest = a[largest] > a[index] ? largest : index; if(largest == index) { break; } swap(a[largest], a[index]); index = largest; left = index * 2 + 1; } } void HeapSort(int a[]) { for(int i = 0 ; i < n; i++) HeapInsert(a,i); int size = n; swap(a[0],a[--size]); while(size > 0) { Heapify(a , 0 ,size); swap(a[0],a[--size]); } } int main() { while(~scanf("%d",&n)) { for(int i =0 ; i < n; i++) scanf("%d",&a[i]); HeapSort(a); for(int i = 0; i < n; i++) printf("%d ",a[i]); printf("\n"); } return 0; } /* 5 5 31 0 1 6 6 9 0 22 1 88 6 */