求:非递归的快速排序代码

robinyin 2003-04-16 02:36:23
rt
...全文
109 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
短歌如风 2003-04-20
  • 打赏
  • 举报
回复
补充:
上面所说的算法是用循环去掉一个递归调用,用栈结构去掉另一个递归调用,由于有对序列不利的判断,递归层数(如果用递归实现的话)最大为32(整数位数),所以栈用一个数组实现。比较简单快速。
短歌如风 2003-04-20
  • 打赏
  • 举报
回复
这里有一个:
非递归快速排序,当待排元素少于16个时改用插入排序;当发现序列对快速排序不利时改用堆排序。用Object Pascal实现.
http://www.chinaithr.com/bbs/bbs11/3.xml
alidiedie 2003-04-20
  • 打赏
  • 举报
回复
也就是用栈模拟递归那的函数调用了
p_s_w 2003-04-19
  • 打赏
  • 举报
回复
不好意思,总是写错点东西,再次修改如下:
int QuickSort( List &L )
{
Stack S;
S.Push( 0 );
S.Push( L.length()-1 );
while ( !S.Empty() )
{
int a, b;
S.Pop( b );
S.Pop( a );
if (a<b)
{
int k = Partition( L, a, b ); // 划分
S.Push( a );
S.Push( k-1 );
S.Push( k+1 );
S.Push( b );
}
}
return 1;
}
p_s_w 2003-04-19
  • 打赏
  • 举报
回复
逻辑上有点欠缺,修改如下:
int QuickSort( List &L )
{
Stack S;
S.Push( 0, L.length()-1 );
while ( !S.Empty() )
{
int a, b;
S.Pop( b );
S.Pop( a );
if (a<b)
{
int k = Partition( L, a, b ); // 划分
S.Push( a );
S.Push( k-1 );
S.Push( k+1 );
S.Push( b );
}
}
return 1;
}
p_s_w 2003-04-19
  • 打赏
  • 举报
回复
int QuickSort( List &L )
{
Stack S;
S.Push( 0, L.length()-1 );
while ( !S.Empty() )
{
int a, b;
S.Pop( b );
S.Pop( a );
int k = Partition( L, a, b ); // 划分
if ( k-1>a )
{
S.Push( a );
S.Push( k-1 );
}
if ( k+1<b )
{
S.Push( k+1 );
S.Push( b );
}
}
return 1;
}
zhaoao 2003-04-19
  • 打赏
  • 举报
回复
不错呀!
格利高里 2003-04-17
  • 打赏
  • 举报
回复
采用heap排序
/*
class: HeapSort 函数

Note: linux/windows下HeapSort快速堆排序函数

Compiler: g++ 3.2.2 vc++6

Modified:
Version: 1.00 Updated: 2003-03-30
修正了原文件中的一个bug,即当array_size为小奇数时的排序错误。
*/

void HeapSort(unsigned int numbers[], int array_size);
static inline void siftDown(unsigned int numbers[], int root, int bottom);
static inline void swap(unsigned int& T1, unsigned int& T2 );

void HeapSort(unsigned int numbers[], int array_size)
{
int i;
for (i = (array_size >> 1); i >= 0; i--)
siftDown(numbers, i, array_size);

for (i = array_size-1; i >= 1; i--)
{
swap(numbers[0], numbers[i]);
siftDown(numbers, 0, i-1);
}
}

void swap(unsigned int& T1, unsigned int& T2 )
{
unsigned int temp = T1;
T1 = T2;
T2 = temp;
}

void siftDown(unsigned int numbers[], int root, int bottom)
{
int maxChild;

while ((root*2 <= bottom))
{
if (root*2 == bottom)
maxChild = root * 2;
else if (numbers[root * 2] > numbers[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;

if (numbers[root] < numbers[maxChild])
{
swap(numbers[root], numbers[maxChild]);
root = maxChild;
}
else
break;
}
}
seeker2003 2003-04-17
  • 打赏
  • 举报
回复
//sort array <int> :pDoc from nStart to nEnd
void QuickSort( int * pDoc,int nStart,int nEnd )
{

int stPivot;

int pivotpos,nLeft,nRight;
nLeft=nStart;nRight=nEnd;
stack < int > stPos;

stPos.push(nLeft);
stPos.push(nRight);
while(!stPos.empty() )
{
nRight=stPos.top();
stPos.pop();
nLeft=stPos.top() ;
stPos.pop();

if(nLeft>=nRight)
continue;

pivotpos=nLeft;
stPivot=pDoc[pivotpos];
int temp;
for(int i=nLeft+1;i<=nRight;i++)
{
//sort by number descend
if(pDoc[i].n>stPivot && ++pivotpos!=i)
{
//swap
temp =pDoc[pivotpos] ;
pDoc[pivotpos]=pDoc[i] ;

pDoc[i]=temp ;
}

}
//sawp
temp =pDoc[pivotpos] ;
pDoc[pivotpos]=pDoc[nLeft] ;

pDoc[nLeft]=temp ;

stPos.push(nLeft);
stPos.push(pivotpos-1);

stPos.push(pivotpos+1);
stPos.push(nRight);
}
}
azuretttc 2003-04-17
  • 打赏
  • 举报
回复
楼上的算法虽然经典,但是堆排序,不是快速排序。
azuretttc 2003-04-16
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;

#define N 10
typedef struct stack {int low,high;} STACK;

void main()
{
int a[N]={3,2,5,1,4,7,6,8,12,7};
STACK s[N*N];
int top;
int low,high,t,i,j;

s[0].low=0;
s[0].high=N-1;
top=1;
while(top>0)
{
i=low=s[--top].low;
j=high=s[top].high;
t=a[low];

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

while(i<j&&a[i]<=t) i++;
if(i<j) a[j--]=a[i];
}
a[i]=t;
if(high>i+1)
{
s[top].high=high;
s[top++].low=i+1;
}
if(i-1>low)
{
s[top].low=low;
s[top++].high=i-1;
}
}

for(i=0;i<N;i++)
cout<<a[i]<<"\t";
}



33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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