-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortMain.cpp
413 lines (388 loc) · 10.1 KB
/
SortMain.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "Heap/Heap.h"
#include "Map/Map.h"
using namespace std;
#define num 100
//冒泡排序
template<class T>
void BubbleSort(vector<T> &x);
//选择排序(1:基本选择排序,2:双端选择排序,other:改进的双端选择排序(min-max+彩虹模式))
template<class T>
void SelectSort(vector<T> &x, int type);
//插入排序(1:线性插入排序,other:折半插入排序)
template<class T>
void InsertSort(vector<T> &x, int type);
//希尔排序
template<class T>
void ShellSort(vector<T> &x);
//计数排序
template<class T>
void CountedSort(vector<T> &x);
//基数排序
template<class T>
void RadixSort(vector<T> &x);
//二叉查找树排序,选做
template<class T>
void BSTSort(vector<T> &x);
//快速排序
template<class T>
void QuickSort(vector<T> &x, int left, int right);
//堆排序
template<class T>
void HeapSort(vector<T> &x);
template<class T>
void Display(vector<T> &x);
template<class T>
void ResetData(vector<T> &x, vector<T> &y);//使用y重置x
//......自行添加其他必要的函数声明
int main() {
vector<int> a(num + 1), b(num + 1);
//有效数据从1开始,也可以设计从0开始,自行根据需要调整
int i;
clock_t start, end;
srand(time(nullptr));//随机数种子初始化
for (i = 1; i <= num; i++) {
a[i] = rand() % 10000 + 1; //随机生成1-10000内的数值作为排序对象
b[i] = a[i];
}
//排序前显示数据
cout << "排序前" << endl;
Display(a);
//冒泡排序
start = clock();
BubbleSort(a);
end = clock();
cout << endl << "冒泡排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//基础选择排序
ResetData(a, b);
start = clock();
SelectSort(a, 1);
end = clock();
cout << "基础选择排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//双端选择排序
ResetData(a, b);
start = clock();
SelectSort(a, 2);
end = clock();
cout << "双端选择排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//改进的双端选择排序
ResetData(a, b);
start = clock();
SelectSort(a, 3);
end = clock();
cout << "改进的双端选择排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//线性插入排序
ResetData(a, b);
start = clock();
InsertSort(a, 0);
end = clock();
cout << "线性插入排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//对分插入排序
ResetData(a, b);
start = clock();
InsertSort(a, 1);
end = clock();
cout << "对分插入排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//希尔排序
ResetData(a, b);
start = clock();
ShellSort(a);
end = clock();
cout << "希尔排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//计数排序
ResetData(a, b);
start = clock();
CountedSort(a);
end = clock();
cout << "计数排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//基数排序
ResetData(a, b);
start = clock();
RadixSort(a);
end = clock();
cout << "基数排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//快速排序
ResetData(a, b);
start = clock();
QuickSort(a, 1, num);
end = clock();
cout << "快速排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//堆排序
ResetData(a, b);
start = clock();
HeapSort(a);
end = clock();
cout << "堆排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
//额外产品:BST排序
ResetData(a, b);
start = clock();
BSTSort(a);
end = clock();
cout << "BST排序后" << endl;
Display(a);
cout << endl << "使用时长:" << double(end - start) << "ms" << endl;
return 0;
}
template<class T>
void Display(vector<T> &x) {
for (int i = 1; i <= num; i++) {
cout << x[i] << " ";
if (i % 15 == 0) cout << endl;
}
}
//使用y重置x
template<class T>
void ResetData(vector<T> &x, vector<T> &y) {
for (int i = 1; i <= num; i++) {
x[i] = y[i];
}
}
template<class T>
void BubbleSort(vector<T> &x) {
for (int i = 2; i <= num; ++i) {
for (int j = 1; j <= num; ++j) {
if (x[j - 1] > x[j]) {
swap(x[j - 1], x[j]);
}
}
}
}
template<class T>
void SelectSort(vector<T> &x, int type) {
switch (type) {
case 1: {
for (int i = 1; i <= num; ++i) {
for (int j = i + 1; j <= num; ++j) {
if (x[i] > x[j]) {
swap(x[i], x[j]);
}
}
}
break;
}
case 2: {
for (int i = 1, j = num; i < j; ++i, --j) {
for (int k = i + 1; k < j; ++k) {
if (x[i] > x[k]) {
swap(x[i], x[k]);
}
if (x[j] < x[k]) {
swap(x[j], x[k]);
}
}
}
break;
}
default: {
for (int i = 1, j = num; i < j; ++i, --j) {
int Min = i;
int Max = j;
for (int k = i + 1; k != j; ++k) {
if (x[Min] > x[k]) {
Min = k;
}
if (x[Max] < x[k]) {
Max = k;
}
}
if (Min != i) {
swap(x[Min], x[i]);
}
if (Max != j) {
swap(x[Max], x[j]);
}
}
break;
}
}
}
template<class T>
void InsertSort(vector<T> &x, int type) {
if (!type) {
for (int i = 2; i <= num; ++i) {
int pos = i;
for (int j = 1; j < i; ++j) {
if (x[j] >= x[i]) {
pos = j;
break;
}
}
for (int j = i; j > pos; --j) {
swap(x[j], x[j - 1]);
}
}
} else {
for (int i = 2; i <= num; ++i) {
int left = 0;
int right = i - 1;
while (left <= right) {
int mid = (right + left) / 2;
if (x[mid] > x[i]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
for (int j = i; j > left; --j) {
swap(x[j], x[j - 1]);
}
}
}
}
template<class T>
void ShellSort(vector<T> &x) {
int gap = num / 2;
while (gap) {
for (int i = 1; i <= gap; ++i) {
for (int j = i + gap; j <= num; j += gap) {
int k = i;
for (; k < j; k += gap) {
if (x[j] < x[k]) {
break;
}
}
for (int l = j; l > k; l -= gap) {
swap(x[l], x[l - gap]);
}
}
}
gap /= 2;
}
}
template<class T>
void CountedSort(vector<T> &x) {
vector<T> count(num + 1);
vector<T> tmp(num + 1);
for (int i = 1; i <= num; ++i) {
for (int j = 1; j <= num; ++j) {
if (i != j && x[i] < x[j]) {
count[j]++;
}
}
}
for (int i = 1; i <= num; ++i) {
tmp[count[i] + 1] = x[i];
}
for (int i = 1; i <= num; ++i) {
x[i] = tmp[i];
}
}
// 求最大位数
template<class T>
int maxBit(vector<T> &x) {
int maxData = x[0];
for (int i = 1; i < x.size(); ++i) {
if (maxData < x[i])
maxData = x[i];
}
int d = 1;
int p = 10;
while (maxData >= p) {
maxData /= 10;
++d;
}
return d;
}
template<class T>
void RadixSort(vector<T> &x) {
int d = maxBit(x);
vector<T> tmp(num + 1);
int count[10];
int i, j, k, radix = 1;
for (i = 1; i <= d; ++i) {
for (j = 0; j < 10; j++) {
count[j] = 0;
}
for (j = 1; j <= num; ++j) {
k = (x[j] / radix) % 10;
count[k]++;
}
for (j = 1; j < 10; ++j) {
count[j] += count[j - 1];
}
for (j = num; j > 0; --j) {
k = (x[j] / radix) % 10;
tmp[count[k]] = x[j];
count[k]--;
}
for (j = 1; j <= num; ++j) {
x[j] = tmp[j];
}
radix *= 10;
}
}
template<class T>
int Paritition(vector<T> &x, int left, int right) {
int pivot = x[left];
while (left < right) {
while (left < right && x[right] >= pivot) {
--right;
}
x[left] = x[right];
while (left < right && x[left] <= pivot) {
++left;
}
x[right] = x[left];
}
x[left] = pivot;
return left;
}
template<class T>
void QuickSort(vector<T> &x, int left, int right) {
if (left < right) {
int pivot = Paritition(x, left, right);
QuickSort(x, left, pivot - 1);
QuickSort(x, pivot + 1, right);
}
}
template<class T>
void HeapSort(vector<T> &x) {
Heap<T> heap;
for (auto item: x) {
heap.add(item);
}
x.clear();
while (!heap.empty()) {
x.push_back(heap.top());
heap.removeTop();
}
}
template<class T>
void BSTSort(vector<T> &x) {
Map<T, int> map;
for (auto item: x) {
map.insert(Pair<T, int>(item, 1));
}
x.clear();
for (auto iter = map.begin(); iter != map.end(); ++iter) {
if (!iter.getPtr()) {
break;
}
x.push_back(iter->getFirst());
}
}