代码找错.(积分全给你们了)

yao4lin 2014-08-10 07:31:32
这段代码为什么会死循环?应该怎么修改?
int main(int argc, char const *argv[])
{
int a[4] ={23,45};
quicksort(a,0,1);
return 0;
}

int partion(int a[],int low,int high)
{
int privt = a[low];
while(low<high)
{
while(low<high&&a[high] >= privt) --high;
a[low++] = a[high];
while(low<high&&a[low] <= privt) ++low;
a[high--] = a[low];
}
a[low] = privt;
return low;
}

void quicksort(int a[],int low,int high){
while(low<high){
int pos = partion(a,low,high);
quicksort(a,low,pos-1);
quicksort(a,pos+1,high);
}

}
...全文
132 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
勤奋的小游侠 2014-08-10
  • 打赏
  • 举报
回复
void quicksort(int a[],int low,int high) 这个函数是值传递,里面怎么改变参数,也不会改变原值的,哎呀,这100分
百曉生 2014-08-10
  • 打赏
  • 举报
回复
用的是C-Free5.0,楼主可以看一下二楼链接的那个代码的Partition函数
cyd54454 2014-08-10
  • 打赏
  • 举报
回复
楼主还是你,
707wk 2014-08-10
  • 打赏
  • 举报
回复
此代码集合有快速排序的参考示例 http://pan.baidu.com/s/17yIcQ
Falleyes 2014-08-10
  • 打赏
  • 举报
回复
一楼使用的是C-Free
yao4lin 2014-08-10
  • 打赏
  • 举报
回复
引用 1 楼 u012421456 的回复:
楼主试过单步调试吗? partion函数没有改变quicksort中low和high的值,所以在quicksort中low一直是小于high的,所以死循环
是不是因为只是拷贝了一个副本进去,partion无论如何都不改变low ,high 的值?
yao4lin 2014-08-10
  • 打赏
  • 举报
回复
引用 1 楼 u012421456 的回复:
楼主试过单步调试吗? partion函数没有改变quicksort中low和high的值,所以在quicksort中low一直是小于high的,所以死循环
对,我就是这一点没理解,ps,你用的是什么调试工具?
百曉生 2014-08-10
  • 打赏
  • 举报
回复
可以在参考一下别人的代码——http://blog.csdn.net/wolinxuebin/article/details/7456330
百曉生 2014-08-10
  • 打赏
  • 举报
回复
楼主试过单步调试吗?

partion函数没有改变quicksort中low和high的值,所以在quicksort中low一直是小于high的,所以死循环

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧