-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortSlow.cc
More file actions
144 lines (125 loc) · 3.12 KB
/
SortSlow.cc
File metadata and controls
144 lines (125 loc) · 3.12 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <algorithm>
#include <format>
#include <functional>
#include <iostream>
#include <random>
#include "vector.hh"
// 冒泡排序
template <typename compar>
void bubble(ns::vector<compar> &A) {
int n = A.size();
bool sorted{false};
while (!sorted) {
sorted = true;
for (int i = 1; i < n; ++i)
if (A[i] < A[i - 1]) {
std::swap(A[i - 1], A[i]);
sorted = false;
}
--n;
}
}
// 单选择排序
template <typename compar>
void selection(ns::vector<compar> &A) {
for (int i(0), sz(A.size()); i < sz; ++i) {
int min{i};
for (int j = i + 1; j < sz; ++j)
if (A[j] < A[min]) min = j;
std::swap(A[i], A[min]);
}
}
// 线性插入排序
template <typename compar>
void insertion(ns::vector<compar> &A) {
for (int i(1), sz(A.size()); i < sz; ++i) {
compar current{A[i]};
int j{i};
for (; j >= 1 && current < A[j - 1]; --j) A[j] = A[j - 1];
A[j] = current;
}
}
// 折半插入排序
template <typename compar>
void leftBisectionInsertion(ns::vector<compar> &A) {
for (int i(1), sz(A.size()); i < sz; ++i) {
compar var{A[i]};
int lo{0}, hi{i - 1};
while (lo <= hi) {
int mid{lo + (hi - lo) / 2};
if (var < A[mid]) hi = mid - 1;
// var >= A[mid]
else
lo = mid + 1;
}
for (int j = i; j > lo; --j) A[j] = A[j - 1];
A[lo] = var;
}
}
template <typename compar>
void rightBisectionInsertion(ns::vector<compar> &A) {
for (int sz(A.size()), i(sz - 1); i >= 0; --i) {
compar var{A[i]};
int lo{i + 1}, hi{sz - 1};
while (lo <= hi) {
int mid{lo + (hi - lo) / 2};
if (var <= A[mid]) hi = mid - 1;
// var > A[mid]
else
lo = mid + 1;
}
for (int j = i; j < hi; ++j) A[j] = A[j + 1];
A[hi] = var;
}
}
// 希尔排序
template <typename compar>
void shell(ns::vector<compar> &A) {
int sz(A.size()), h{1};
// 3 * h + 1 <= sz
while (h < sz / 3) h = 3 * h + 1;
while (h >= 1) {
for (int i = h; i < sz; ++i) {
compar var{A[i]};
int j{i};
while (j >= h && A[j - h] > var) {
A[j] = A[j - h];
j = j - h;
}
A[j] = var;
}
h = h / 3;
}
}
template <class T>
void printSort(std::function<void(ns::vector<T> &)> F, ns::vector<T> A) {
F(A);
std::ranges::for_each(A, [](auto x) { std::cout << x << '\t'; });
std::cout << '\n';
std::cout << std::format("{}",
std::ranges::is_sorted(A) ? "Sorted" : "Unsorted");
;
std::cout << "\n\n";
}
int main() {
std::mt19937 mt(std::random_device{}());
std::uniform_int_distribution rand(10, 99);
ns::vector<int> A(16);
for (auto &e : A) {
e = rand(mt);
std::cout << e << '\t';
}
std::cout << "\n\n";
std::cout << "bubble\n";
printSort<int>(bubble<int>, A);
std::cout << "selection\n";
printSort<int>(selection<int>, A);
std::cout << "insertion\n";
printSort<int>(insertion<int>, A);
std::cout << "leftBisectionInsertion\n";
printSort<int>(leftBisectionInsertion<int>, A);
std::cout << "rightBisectionInsertion\n";
printSort<int>(rightBisectionInsertion<int>, A);
std::cout << "shell\n";
printSort<int>(shell<int>, A);
}