快速排序的非递归哪里不对吗?

qifeifei 2010-05-29 10:36:41
#include "iostream"
using namespace std;
typedef struct node
{
int start;
int end;
}*Node;
void QuickSort(int a[],int l,int h)
{
int temp,low,high,top=-1;
Node stack[20];
node *p=(Node)malloc(sizeof(node));
p->start=l;
p->end=h;
stack[++top]=p;

while(top>=0)
{
low=stack[top]->start;
high=stack[top]->end;
top--;
temp=a[low];
if(low<high)
{
while(low<high)
{
while(low<high&&a[high]>=temp)
high--;
a[low]=a[high];
while(low<high&&a[low]<=temp)
low++;
a[high]=a[low];
}
a[low]=temp;
p->start=l;p->end=high-1;stack[++top]=p;
p->start=low+1;p->end=h;stack[++top]=p;
}
}
}
int main(int argc, char* argv[])
{
int a[]={5,6,8,4,1,7,2,3,9,11,10};
QuickSort(a,0,10);
for(int i=0;i<11;i++)
cout<<a[i]<<' ';
return 0;
}
...全文
137 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qifeifei 2010-05-31
  • 打赏
  • 举报
回复
终于知道哪里错了,还是要靠自己啊。
sduxiaoxiang 2010-05-29
  • 打赏
  • 举报
回复
p->start=l;p->end=high-1;stack[++top]=p;
p->start=low+1;p->end=h;stack[++top]=p;

这的问题吧

stack存储的是指针p,指向的是一个地方。。stack中相邻两个是一样的。。前面部分不会快排
每次存储都重新new node 再赋值运行吧
qifeifei 2010-05-29
  • 打赏
  • 举报
回复
5楼的你说的没错,我那边确实有问题,但是我改成这样了,为什么跳不出循环啊,求救啊。
#include "stdafx.h"
#include "iostream"
using namespace std;

//下面是非递归的方法来实现快速排序
struct node
{
int start;
int end;
}s[20];
void QuickSort(int a[],int l,int h)
{
int temp,low,high,top=-1;
s[++top].start=l;
s[top].end=h;

while(top>-1)//调试运行后可以看到a数组里是已经排序好了的,但是并没有输出结果,为什么。
{
low=s[top].start;
high=s[top].end;
top--;
temp=a[low];
if(low<high)
{
while(low<high)
{
while(low<high&&a[high]>=temp)
high--;
a[low]=a[high];
while(low<high&&a[low]<=temp)
low++;
a[high]=a[low];
}
a[low]=temp;
s[++top].start=low+1;s[top].end=h;
s[++top].start=l;s[top].end=high-1;
}
}
}

int main(int argc, char* argv[])
{
int a[]={5,6,8,4,1,7,2,3,9,11,10};
QuickSort(a,0,10);
for(int i=0;i<11;i++)
cout<<a[i]<<' ';
return 0;
}

wibnmo 2010-05-29
  • 打赏
  • 举报
回复
不是偶写滴,前几天在CSDN上搜到的,忘了谁写的了。帖出来讨论讨论。

void qsort(int a[],int l,int r)
{
while(l < r)
{
int q=a[l],temp;
int i=l,j=r;

while (i < j)
{
while(j>i && a[j]>q) --j;
if(j > i) a[i++] = a[j];

while(j>i && a[i]<q) ++i;
if (j > i) a[j--] = a[i];
}

a[i] = q;
qsort(a, l, i-1);
l = i + 1;
}
}


int main()
{
int a[] = {3,4,8,7,6,5,9};
int i;
qsort(a, 0, 6);

for (i=0; i<7; i++)
printf("%d ",a[i]);
getch();
return 0;
}
ljx87085210 2010-05-29
  • 打赏
  • 举报
回复
看了一会有点晕呵呵,node *p=(Node)malloc(sizeof(node));没看到有free的地方。还有就是没有注释哦,最好加上,虽然这个程序代码不多,但是养成习惯吧。不然每次自己看都要重新想逻辑。
qifeifei 2010-05-29
  • 打赏
  • 举报
回复
递归的很简单,我要的是非递归算法,清楚我的意思没?
qq120848369 2010-05-29
  • 打赏
  • 举报
回复
相反,快排我支持递归写法.
wbgxx 2010-05-29
  • 打赏
  • 举报
回复
int split(int data[], int low, int high)
{
int i = low;
int k;
int x = data[low];
for (k=low+1; k<=high; k++)
{
if (x<=data[k])
{
i = i + 1;
if (i!=k)
swap(data[i], data[k]);
}
}

swap(data[low], data[i]);
return i;
}

void quick_sort(int data, int low, int high)
{
int k;
if (low < high)
{
k = split(data, low, high);
quick_sort(data, low, k-1);
quick_sort(data, k+1, high);
}
}

69,336

社区成员

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

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