堆排序那里错了?
程序编译没错误,运行有错~我猜可能是内存分配的问题吧
#define N 10
#define EXC(x,t,y) (t=x,x=y,y=t)
#include<stdio.h>
max_heap(int,int[],int);
main()
{int a[N+1],t,h_size=N,i;
a[0]=0;
for(i=1;i<=N;i++)
scanf("%d",a[i]);
for(i=N/2;i>0;i--)
max_heap(i,a,N);
for(i=N;i>1;i--)
{
{EXC(a[i],t,a[1]);
h_size-=1;
max_heap(1,a,h_size);
}
}
for(i=N;i>=1;i--)
printf("%d",a[i]);
}
max_heap(int i,int a[],int h_size)
{int L=2*i,R=2*i+1,t;
int largest;
if(L<h_size&&a[L]>a[i])
largest=L;
if(R<h_size&&a[R]>a[largest])
largest=R;
if(i!=largest)
{EXC(a[i],t,a[largest]);
max_heap(largest,a,h_size);
}
}