#include <stdio.h>
void swap(int* a,int* b)
{
*a=*a^*b;
*b=*b^*a;
*a=*a^*b;
}
void heapAdjust(int* arr,int s,int m)
{
int rc=arr[s]; int j;
for(j=2*s;j<=m;j*=2)
{
if(j<m&&arr[j]<arr[j+1]) j++;
if(rc>=arr[j]) break;//There need not to adjust the heap
arr[s]=arr[j]; s=j;
}
arr[s]=rc; int i;
}
void heapSort(int* arr,int len)
{
int i;
for(i=len/2-1;i>=0;i--)
heapAdjust(arr,i,len);
for(i=len-1;i>0;i--)
{
swap(&arr[0],&arr[i]);
heapAdjust(arr,0,i-1);
}
}
int main()
{
int arr[10]={1,3,5,7,9,2,4,6,8,0};
int i=0;
/*for(i=4;i>=0;i--)
{
heapAdjust(arr,i,10);
}
for(i=9;i>0;i--)
{
swap(&arr[0],&arr[i]);
heapAdjust(arr,0,i-1);
}*/
heapSort(arr,10);
for(i=0;i<10;i++)
printf("%d\t",arr[i]);
printf("\n");
}