当前位置:网站首页>Non recursive writing of quick sort
Non recursive writing of quick sort
2022-06-11 21:57:00 【Code loving students】
Learned the recursive algorithm of quick sorting , We may sigh that recursion is really a good thing , But in different cases , We need to consider a lot of things , If recursion is used under the influence of big data, stack overflow occurs , At this time, we should consider using non recursive to realize quick sorting .
The code implementation is as follows :
// Non recursive implementation of quick sorting
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);
}Here we use the stack to help us achieve a recursive effect .
Implementation steps :
1. Interval passed in left,right
2. Take out left,right, And get the subscript of the reference number
3. Judge the interval [left,pivot-1] [pivot+1,end] Whether there is , If it exists, it is stored in the stack
4. Repeat the above operation , Until there are no elements in the stack
Remember to destroy the stack after using it .
边栏推荐
- Matlab: solution of folder locking problem
- R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model
- Leetcode-32- longest valid bracket
- Introduction à endnotex9 et instructions pour les tutoriels de base
- Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
- Huawei equipment configuration hovpn
- Leetcode-43- string multiplication
- 类和对象(1)
- 行而不辍,未来可期|云扩科技入选上海市专精特新企业
- Go IO module
猜你喜欢

R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型

Building a custom CNN model: identifying covid-19

超标量处理器设计 姚永斌 第2章 Cache --2.3 小节摘录

Classes and objects (3)

每日一题 - 罗马数字转整数

如何查看win系统的安装日期

快速排序的优化

超標量處理器設計 姚永斌 第2章 Cache --2.4 小節摘錄

Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?

Leetcode-76- minimum covering substring
随机推荐
华为设备配置H-VPN
Parker plunger pump pv180r1k1t1nmmc
206.反转链表
C语言实现八种排序(1)
行而不辍,未来可期|云扩科技入选上海市专精特新企业
R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model
67. 二进制求和
2022-02-28(2)
The shortcomings of the "big model" and the strengths of the "knowledge map"
Leetcode - 第2天
Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)
不使用加减乘除做加法
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
JVM | local method interface; Native Method Stack
RPA超自动化 | 农耕记携手云扩加速财务智能化运营
Huawei equipment configuration h-vpn
Go OS module
C语言实现八种排序 - 归并排序
JVM class loader; Parental delegation mechanism
如何利用RPA机器人开启货代行业数字化转型第一步?