用堆阵实现数组排序出错...
时间:2008-06-12 15:10:12
来源:论坛整理 作者: 编辑:chinaitzhe
#include <stdio.h>
void siftdown(int a[],int i,int n)
{
int temp;
int c;
temp=a[i];
while(2*i <=n)
{
c=2*i;
if((c <n)&&(a[c 1]>a[c]))
c =1;
if(a[c]>temp)
a[i]=a[c];
else
break;
i=c;
}
a[i]=temp;
}
int main()
{
int temp;
int a[5]={0};
int i;
for(i=1;i <=5;i )
scanf("%d",a i);
for(i=5/2;i>0;i--)
{
siftdown(a,i,5);
}
for(i=5;i>1;)
{
temp=a[1];
a[1]=a[i];
a[i]=temp;
i--;
if(i>1)
{
siftdown(a,1,i);
}
}
for(i=1;i <=5;i )
printf("%d ",a[i]);
return 0;
}
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ for(i=1;i <=5;i ) /* a[5]下标是0-4,应该改为 for(i = 0;i < 5;i )*/ scanf("%d",a i);
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ …… for(i=5;i>1;) /* 显然还是我刚才说的问题,a[5]在哪里??要取最后一个元素用a[4],改为for(i=4;i>=0;)*/ …… for(i=1;i <=5;i ) /* 还是1楼说的那个错误 */ printf("%d ",a[i]);
网友回复:
当局者迷...
thank you very much~~
关键字:堆阵,实现,数组,排序,出错,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面