我这个堆排有什么问题?
我这个堆排有什么问题?
#include<stdio.h>
void buildheap(int *r,int k,int m){
int i,j,x;
i=k;j=2*i;
x=r[i];
while(j<=m){
if(j<m&&r[j]<r[j+1])j++;
if(x<r[j])
{r[i]=r[j];
i=j;
j=2*i;
}
else {j=m*i;}
r[i]=x;}}
void heapsort (int *r,int n){
int i,w;
for(i=n/2;i>=1;i--)
buildheap(r,i,n);
for(i=n;i>=2;i--){
w=r[i];
r[i]=r[1];
r[1]=w;
buildheap(r,i-1,1);}}
main(){
int a[20000],n;
long i;
FILE *in,*out;
in=fopen("hmx0979.in","r");
out=fopen("hmx0979.out","w");
fscanf(in,"%ld",&n);
for(i=0;i<n;i++)
fscanf(in,"%ld",&a[i]);
heapsort(a,n);
for(i=0;i<n;i++)
fprintf(out,"%ld ",a[i]);
}