用快速排序对100万个记录排序

ZOthello 2011-10-02 09:14:12
我用快速排序对100万个记录排序,序列大致有序,怎么得不到结果,排着排着就结束了,是内存空间的限制还是咋了?是不是和快速排序中使用递归调用有关系?
...全文
243 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZOthello 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jsjrj01 的回复:]

100M的数量级,结构体+递归,很有可能内存都不大够,小规模数据没事,那么堆栈估计溢出了吧
[/Quote]

是排序函数对于特定的序列会出问题,没有溢出,我是在堆上分配的空间。换了一个快速排序算法就解决了
yjjlyyj151 2011-10-03
  • 打赏
  • 举报
回复
++[Quote=引用 10 楼 luciferisnotsatan 的回复:]

难道是递归太深了,栈溢出了?
一般栈溢出会输出 stack overflow。lz有没有这个输出?
栈默认1M,可以自己加大。
[/Quote]
别逗我乐 2011-10-03
  • 打赏
  • 举报
回复
100M的数量级,结构体+递归,很有可能内存都不大够,小规模数据没事,那么堆栈估计溢出了吧
luciferisnotsatan 2011-10-03
  • 打赏
  • 举报
回复
难道是递归太深了,栈溢出了?
一般栈溢出会输出 stack overflow。lz有没有这个输出?
栈默认1M,可以自己加大。
AndyZhang 2011-10-03
  • 打赏
  • 举报
回复
C 有qsort
C++ 有sort
ljhhh0123 2011-10-02
  • 打赏
  • 举报
回复
C库函数就是做这种事的。但必须写好自己的排序函数。
#include <time.h>
#include <stdlib.h>
struct point{
int x;
int y;
};
int point_cmp(const void* e1,const void *e2){
int v1=((struct point*)e1)->x;
int v2=((struct point*)e2)->x;
return (v1<v2)? -1 : (v1>v2) ? 1 : 0;
}
int main()
{
struct point p[10];
int i;
srand(time(NULL));
for(i=0;i<10;i++){
p[i].x=rand();
printf("p[%d].x=%d\n",i,p[i].x);
}
printf("sort.......\n");
qsort((void*)p,(size_t)10,sizeof(struct point),point_cmp);
for(i=0;i<10;i++){
printf("p[%d].x=%d\n", i,p[i].x);
}
return 0;
}
ZOthello 2011-10-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ljljlj 的回复:]

用标准C库qsort函数排序看有问题没。没有十分的把握,不要用自己写的排序函数。
[/Quote]

我要对结构体进行排序,没法用C库函数
ljhhh0123 2011-10-02
  • 打赏
  • 举报
回复
用标准C库qsort函数排序看有问题没。没有十分的把握,不要用自己写的排序函数。
ZOthello 2011-10-02
  • 打赏
  • 举报
回复
根据结构体的row关键字来排序,小规模数据没问题大规模的就排不了
ZOthello 2011-10-02
  • 打赏
  • 举报
回复
int partition(SPARSENODE arr[],int low,int high)
{
arr[0].row=arr[low].row;
arr[0].col=arr[low].col;
arr[0].weight=arr[low].weight;
while(low<high){
while((low<high)&&(arr[high].row>=arr[0].row)) high--;
arr[low].col=arr[high].col;
arr[low].row=arr[high].row;
arr[low].weight=arr[high].weight;
while((low<high)&&(arr[low].row<=arr[0].row)) low++;
arr[high].col=arr[low].col;
arr[high].row=arr[low].row;
arr[high].weight=arr[low].weight;
}
arr[low].row=arr[0].row;
arr[low].col=arr[0].col;
arr[low].weight=arr[0].weight;
//printf("pivotindex:%d\n",low);
return low;
}


void si_sort(SPARSENODE arr[],int low,int high)
{
int pivotindex;
if(low<high){
pivotindex=partition(arr,low,high);
si_sort(arr,low,pivotindex-1);
si_sort(arr,pivotindex+1,high);
}
}


我猜测可能是输入的数据导致出现死循环了
尘缘udbwcso 2011-10-02
  • 打赏
  • 举报
回复
100万
最好不要用递归吧
Athenacle_ 2011-10-02
  • 打赏
  • 举报
回复
结束的表现是什么?有跳错误吗?
有可能是栈溢出
mengmingtao 2011-10-02
  • 打赏
  • 举报
回复
100万递归的话也不过20重而已。
贴代码吧

69,382

社区成员

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

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