void creatheap(int *heap,int root,int index)
{
int i,j;
int temp;
int finish;
j=2*root;
temp=heap[root];
finish=0;
while(j<=index&&finish==0)
{
if(j<index)
if(heap[j]<heap[j+1])
j++;
if(temp>=heap[j])
finish=1;
else
{
heap[j/2]=heap[j];
j=2*j;
}
}
heap[j/2]=temp;
}
void heapsort(int *heap,int index)
{
int i,j,temp;
for(i=(index/2);i>=1;i--)
creatheap(heap,i,index);
for(i=index-1;i>=1;i--)
{
temp=heap[i+1];
heap[i+1]=heap[1];
heap[1]=temp;
creatheap(heap,1,i);
printf("sorting process:");
for(j=1;j<=index;j++)
printf(" %d",heap[j]);
printf("\n");
}
}
void main()
{
int list[20];
int node;
int i,index;
printf("\n please input the values you want to sort (exit for 0):\n");
list[0]=0;
index=1;
scanf("%d",&node);
while(node!=0)
{
list[index]=node;
index=index+1;
scanf("%d",&node);
}
index--;
printf("source values:");
for(i=1;i<=index;i++)
printf(" %d",list[i]);
printf("\n\n");
heapsort(list,index);
printf("\nsorting result:");
for(i=1;i<=index;i++)
printf(" %d",list[i]);
printf("\n");
}