-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.std_sort.cpp
107 lines (98 loc) · 2.87 KB
/
7.std_sort.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*************************************************************************
> File Name: 7.std_quick_sort.cpp
> Author: 任博宇
> Mail: [email protected] / [email protected]
> Created Time: 四 2/10 17:23:34 2022
************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
const int __threshold = 16;
void quick_sort_v1(vector<int> &nums, int l, int r) {
int x = l, y = r, z = nums[l];
while (x < y) {
while (x < y && nums[y] >= z) y--;
if (x < y) nums[x++] = nums[y];
while (x < y && nums[x] < z) x++;
if (x < y) nums[y--] = nums[x];
}
nums[x] = z;
quick_sort_v1(nums, l, x - 1);
quick_sort_v1(nums, x + 1, r);
return ;
}
void quick_sort_v2(vector<int> &nums, int l, int r) {
while (l < r) {
int x = l, y = r, z = nums[l];
while (x < y) {
while (x < y && nums[y] >= z) y--;
if (x < y) nums[x++] = nums[y];
while (x < y && nums[x] <= z) x++;
if (x < y) nums[y--] = nums[x];
}
// 一次partition之后x,y都在同一个位置
nums[x] = z;
// 右侧还用递归
quick_sort_v2(nums, x + 1, r);
// 但是因为重设了r的值为左侧区间
// 并且满足while循环条件
// 左侧相当于用循环做
// 这样就是相当于在一层的栈空间内完成
// 少了一倍的栈空间展开
r = x - 1;
}
return ;
}
int getMid(int a, int b, int c) {
if (a > b) swap(a, b);
if (a > c) swap(a, c);
if (b > c) swap(b, c);
return b;
}
void __quick_sort_v3(vector<int> &nums, int l, int r) {
while (r - l > __threshold) {
int x = l, y = r, z = getMid(nums[l], (nums[(l + r) >> 1]), nums[r]);
do {
while (nums[x] < z) x++;
while (nums[y] > z) y--;
if (x <= y) {
swap(nums[x], nums[y]);
x++, y--;
}
} while (x <= y);
__quick_sort_v3(nums, x + 1, r);
r = y;
}
}
void final_insert_sort(vector<int> &nums, int l, int r) {
// 无监督
// 先找到最小值放到第一位去
// ind存最小值的下标
int ind = l;
for (int i = l + 1; i <= r; i++) {
if (nums[i] < nums[ind]) ind = i;
}
while (ind > l) {
swap(nums[ind], nums[ind - 1]);
ind--;
}
// 还记得我们是分成了很多组么
// 组间其实是内省排序
// 组内是插入排序(小数据)
for (int i = l + 2; i <= r; i++) {
int j = i;
while (nums[j] < nums[j - 1]) {
swap(nums[j], nums[j - 1]);
j--;
}
}
return ;
}
void quick_sort_v3(vector<int> &nums, int l, int r) {
__quick_sort_v3(nums, l, r);
final_insert_sort(nums, l, r);
return ;
}
int main() {
return 0;
}