当前位置:网站首页>快速排序的非递归写法
快速排序的非递归写法
2022-06-11 21:36:00 【爱学代码的学生】
学习了快速排序的递归算法,我们可能会感叹递归真是一个好东西,但是在不同的情况下,我们需要考虑很多东西,如果在大数据的影响下使用递归发生了栈溢出,那我们这时候就应该考虑使用非递归来实现快速排序。
代码实现如下:
//非递归实现快速排序
void QuickSort3(int*a,int left,int right){
ST s;
InitStack(&s);
StackPush(&s,left);
StackPush(&s,right);
while(!StackEmpty(s)){
int end=StackGet(s);
StackPop(&s);
int start=StackGet(s);
StackPop(&s);
int pivot=Partition1(a,start,end);
if(start<pivot-1){
StackPush(&s,start);
StackPush(&s,pivot-1);
}
if(pivot+1<end){
StackPush(&s,pivot+1);
StackPush(&s,end);
}
}
StackDestory(&s);
}这里我们使用了栈来帮助我们完成类似递归的效果。
实现步骤:
1. 传入了区间left,right
2. 取出left,right,并且得到基准数的下标
3. 判断区间[left,pivot-1] [pivot+1,end]是否存在,若存在则存入栈
4. 重复上述操作,直到栈中没有元素
记得使用完了栈要进行销毁。
边栏推荐
- How does the chief financial officer of RPA find the "super entrance" of digital transformation?
- Website online customer service system Gofly source code development log - 2 Develop command line applications
- LeetCode-76-最小覆盖子串
- Codeworks round 744 (Div. 3) problem solving Report
- 二分查找 - 学习
- Relatively perfect singleton mode
- 类和对象(2)
- Customer information management software
- CANN编码的一些报错汇编
- As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
猜你喜欢

继承的所有特征
![[Part 14] source code analysis and application details of completionservice class [key]](/img/41/9f5383d6eafb32723e29c15da3a1af.jpg)
[Part 14] source code analysis and application details of completionservice class [key]

多态的所有特征

In the future, cloud expansion technology is expected to be selected as a specialized, special and new enterprise in Shanghai

How does the chief financial officer of RPA find the "super entrance" of digital transformation?

Usage of esp32c3 Arduino Library

Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?

Endnotex9 introduction and basic tutorial instructions

RPA+低代码助推品牌电商启新创变、重启增长

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze
随机推荐
Website online customer service system Gofly source code development log - 2 Develop command line applications
字符串复制函数
JVM | introduction
RPA丨首席财务官如何找到数字化转型“超级入口”?
Analysis on the development history and market development status of China's system integration industry in 2020 [figure]
JVM|运行时数据区;程序计数器(PC寄存器);
JS performs non empty judgment on various data types of the returned data.
关于斜率优化
A collection of commonly used open source data sets for face recognition
How to create the simplest SAP kyma function
杭电中超9 1006 Guess the Weight
servlet获取表单数据
JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
Only 38 years old! Zhou Chuan, a researcher of the Chinese Academy of Sciences, died unfortunately. Rao Yi sent a document to mourn: he guided me when he was still my student
apache 本地多端口配置
Diary at 16:29:41 on June 9, 2022
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
Codeforces Round #740 Div. 2 解题报告
Redis basic data type (Zset) ordered collection
剑指Offer 29.顺时针打印矩阵