import java.util.*; public class Main { public static void main(String[] arg){ Scanner in =new Scanner(System.in); int n=in.nextInt(); int[] nums=new int[n]; for(int i=0;i<n;i++){ nums[i]=in.nextInt(); } heapSort(nums); } public static void heapSort(int[] nums){ int n=nums.length; for(int i=n/2-1;i>=0;i--){ //从最后一个非叶子节点开始调整 heapAdjust(nums,i,n); } for(int i=n-1;i>0;i--){ swap(nums,0,i); heapAdjust(nums,0,i); } for(int i=0;i<n;i++){ System.out.print(nums[i]+" "); } } //将根节点沉入已构建的大顶堆 public static void heapAdjust(int[] nums,int i,int n){ for(int k=2*i+1;k<n;k=2*k+1){//找左节点 if(k+1<n&&nums[k+1]>nums[k]){ //右节点更大则换为右节点 更大的往上浮 k++; } if(nums[k]>nums[i]){ swap(nums,i,k); i=k; }else break; } } public static void swap(int[] nums,int i,int j){ int temp=nums[i]; nums[i]=nums[j]; nums[j]=temp; return; } }