-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro_sort.cpp
More file actions
55 lines (50 loc) · 1.25 KB
/
intro_sort.cpp
File metadata and controls
55 lines (50 loc) · 1.25 KB
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
class Solution {
public:
void insertion_sort(vector<int> &v, int l, int h) {
for (int i = l + 1; i <= h; ++i) {
int j = i - 1;
while (j >= 0 and v[j] > v[j + 1]) {
swap(v[j], v[j + 1]);
--j;
}
}
}
void heap_sort(vector<int> &v, int l, int h) {
make_heap(begin(v) + l, begin(v) + h + 1);
sort_heap(begin(v) + l, begin(v) + h + 1);
}
int partition(vector<int> &v, int l, int h) {
swap(v[l + (rand() % (h - l + 1))], v[h]);
int i = l - 1;
for (int j = l; j <= h; ++j) {
if (v[j] <= v[h]) {
swap(v[++i], v[j]);
}
}
return i;
}
void intro_sort(vector<int> &v, int max_depth, int l, int h) {
if (l >= h)
return;
if (h - l + 1 <= 16) {
insertion_sort(v, l, h);
} else if (max_depth == 0) {
heap_sort(v, l, h);
} else {
int p = partition(v, l, h);
intro_sort(v, max_depth - 1, l, p - 1);
intro_sort(v, max_depth - 1, p + 1, h);
}
}
void quick_sort(vector<int> &v, int l, int h) {
if (l >= h)
return;
int p = partition(v, l, h);
quick_sort(v, l, p - 1);
quick_sort(v, p + 1, h);
}
vector<int> sortArray(vector<int> &nums) {
intro_sort(nums, 32, 0, nums.size() - 1);
return nums;
}
};