当前位置:网站首页>面试题快速排序 递归和非递归实现
面试题快速排序 递归和非递归实现
2022-06-09 10:05:00 【一枚小菜程序员】
递归
void Recursion_sort(vector<int>& nums, int l, int r) {
if (l + 1 >= r) {
return;
}
int first = l, last = r - 1, temp = nums[first];//基准
while (first < last) {
while (first < last && temp <= nums[last]) {
last--;
}
if (first < last)
nums[first] = nums[last];
while (first < last && temp >= nums[first]) {
first++;
}
if(first < last)
nums[last] = nums[first];
}
nums[first] = temp;
Recursion_sort(nums, l, first);//区间 [0---mid]
Recursion_sort(nums, first+1, r);//区间 [mid+1---size]
}
非递归
int Recursion_sort(vector<int>& nums, int l, int r) {
if (l >= r) {
return -1;
}
int first = l, last = r , temp = nums[first];//基准
while (first < last) {
while (first < last && temp <= nums[last]) {
last--;
}
if (first < last)
nums[first] = nums[last];
while (first < last && temp >= nums[first]) {
first++;
}
if(first < last)
nums[last] = nums[first];
}
nums[first] = temp;
return first;
}
void NRecursion_sort(vector<int>& nums) {
stack<int>st;
st.push(0), st.push(nums.size() - 1);
while (!st.empty()) {
int r = st.top();
st.pop();
int l = st.top();
st.pop();
int mid = Recursion_sort(nums,l, r);
if (mid - 1 > l) {
st.push(l), st.push(mid - 1);
}
if (mid + 1 < r) {
st.push(mid+1), st.push(r);
}
}
}
测试:
int main()
{
vector<int>nums={ 6,2,5,8,7,9,4,5,1,3,5,4,0 };
for (auto c : nums) {
cout << c << " ";
}
cout << endl;
NRecursion_sort(nums);
Recursion_sort(nums, 0, nums.size());
for (auto c : nums) {
cout << c << " ";
}
return 0;
}边栏推荐
- How to pass the MySQL database header song training task stored procedure?
- 叁拾壹- NodeJS简单代理池(合) 之 MongoDB 链接数爆炸了
- WPF 实现带明细的环形图表
- How does cloud based LDAP save traditional LDAP?
- Le nombre de liens mongodb pour le pool d'agents simples de nodejs a explosé
- Is flush app transaction safe
- [genius_platform software platform development] lesson 36: definition of maximum value macro of built-in data type
- 自定义权限校验方法
- Configuration based permission control
- 1340. jumping game v-dynamic planning plus DFS
猜你喜欢

Kubernets chapitre 7: POD Advanced, Controller Advanced, Resource and Dashboard

Interaction between C language and Lua (practice 2)

UnsupportedOperationException异常解决

AI 考生挑战高考作文获 48 分;IBM 宣布退出俄罗斯市场,已暂停在俄所有业务;OpenCV 4.6 发布|极客头条...

Test development engineers who don't work overtime are not good programmers? It may not be a stupid bird, but it always flies first

Kubernetes第七篇:Pod進階、Controller進階、Resource和Dashboard

使用source tree 误删远程以及本地仓库恢复办法

"When you are no longer a programmer, many things will get out of control" -- a dialogue with SUSE CTO, the world's largest independent open source company

Jincang of the National People's Congress won the recognition of "key software enterprises encouraged by the state" again

【PHP】代码复用特殊类Trait的简要说明和相关举例
随机推荐
多线程中thread::join()和thread::detach()的区别
其它权限校验方法
crash问题
Is it safe to open an account at flush
Lua调用原理展示(Lua堆栈)
Is flush app transaction safe
How does cloud based LDAP save traditional LDAP?
15 must know MySQL index failure scenarios, stop stepping on the pit!
Go strconv package
How many points can you get if the programmer's college entrance examination paper is exposed?
Stop watch today
"When you are no longer a programmer, many things will get out of control" -- a dialogue with SUSE CTO, the world's largest independent open source company
[genius_platform software platform development] lesson 35: UDP for cross network segment broadcasting
人大金仓再次荣获“国家鼓励的重点软件企业”认定
UnsupportedOperationException异常解决
跨域请求的问题
数学公式显示
PIC模拟(Particle-in-Cell Codes) (任务A和任务C)
Micronet: image recognition with very low flop
978. longest turbulent subarray