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

robinyin 2003-04-16 02:36:23
rt
...全文
102 11 打赏 收藏 转发到动态 举报
写回复
用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";
}



1.算法是程序的灵魂,优秀的程序在对海量数据处理时,依然保持高速计算,就需要高效的数据结构和算法支撑。2.网上数据结构和算法的课程不少,但存在两个问题:1)授课方式单一,大多是照着代码念一遍,数据结构和算法本身就比较难理解,对基础好的学员来说,还好一点,对基础不好的学生来说,基本上就是听天书了2)说是讲数据结构和算法,但大多是挂羊头卖狗肉,算法讲的很少。 本课程针对上述问题,有针对性的进行了升级 3)授课方式采用图解+算法游戏的方式,让课程生动有趣好理解 4)系统全面的讲解了数据结构和算法, 除常用数据结构和算法外,还包括程序员常用10大算法:二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法、马踏棋盘算法。可以解决面试遇到的最短路径、最小生成树、最小连通图、动态规划等问题及衍生出的面试题,让你秒杀其他面试小伙伴3.如果你不想永远都是代码工人,就需要花时间来研究下数据结构和算法。教程内容:本教程是使用Java来讲解数据结构和算法,考虑到数据结构和算法较难,授课采用图解加算法游戏的方式。内容包括: 稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。学习目标:通过学习,学员能掌握主流数据结构和算法的实现机制,开阔编程思路,提高优化程序的能力。

33,008

社区成员

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

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