两种风格的快速排序 Quick Sort
【摘要】
啊哈算法中的quick_sort.cpp
//
// Created by jal on 18-9-2.
//
#include <bits/stdc++.h>
using namesp...
啊哈算法中的quick_sort.cpp
//
// Created by jal on 18-9-2.
//
#include <bits/stdc++.h>
using namespace std;
void quick_sort(int*a, int left, int right){
if(left > right){
return;
}
int j = right;
int i = left;
while(i != j){
while(a[j] >= a[left] && i < j)j--;
while(a[i] <= a[left] && i < j)i++;
if(i < j){
swap(a[i],a[j]);
}
}
swap(a[left], a[j]);
quick_sort(a, left, j-1);
quick_sort(a, j+1, right);
}
int main() {
int a[10] = {4, 3, 5, 3, 6, 1, 2, 9, 8, 7};
int len = sizeof(a) / sizeof(a[0]);
quick_sort(a,0, len-1);
copy(a, a + len, ostream_iterator<int>(cout, " "));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
慕课网中的quick_sort.cpp
//
// Created by jal on 18-9-2.
//
#include <bits/stdc++.h>
using namespace std;
int partion(int *a,int left,int right){
int key = a[left];
int i = left;
for(int j = i+1; j <= right; j++){
if(a[j] < key){
swap(a[i+1], a[j]);
i++;
}
}
swap(a[left], a[i]);
return i;
}
void quick_sort(int*a, int left, int right){
if(left >= right){
return;
}
int p = partion(a, left, right);
quick_sort(a, left, p-1);
quick_sort(a, p+1, right);
}
int main() {
int a[10] = {4, 3, 5, 3, 6, 1, 2, 9, 8, 7};
int len = sizeof(a) / sizeof(a[0]);
quick_sort(a,0, len-1);
copy(a, a + len, ostream_iterator<int>(cout, " "));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
文章来源: blog.csdn.net,作者:爱玲姐姐,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jal517486222/article/details/82313850
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)