#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void headAdjust(int A[],int i,int n){
int temp,temp2;
temp=A[i];
for(int j=2*i;j<n-1;j=j*2){
if(j+1<=n-1 && A[j]<A[j+1]){
j++;
}
if(temp<A[j]){
temp2=A[i];
A[i]=A[j];
A[j]=temp2;
i=j;
}
else
break;
}
}
void buildMaxHeap(int A[],int n){ //建立大根堆
int i;
for(i=n/2-1;i>=0;i--){
headAdjust(A,i,n);
}
}
void heapSort(int A[],int n){
int temp;
buildMaxHeap(A,n);
for(int i=n-1;i>=0;i--){
temp=A[i];
A[i]=A[0];
A[0]=temp;
headAdjust(A,0,i);
}
}
void Printf(int A[],int n){
for(int i=0;i<n;i++){
printf("%d ",A[i]);
}
}
int main(){
int A[11]={6,7,8,5,1,4,2,3,15,7,6};
int n=11;
heapSort(A,n);
Printf(A,n);
return 0;
}