-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnum_10.cpp
More file actions
463 lines (441 loc) · 10.4 KB
/
num_10.cpp
File metadata and controls
463 lines (441 loc) · 10.4 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<vector>
#include<chrono>
using namespace std;
class my_int
{
public:
my_int(int num)
{
number = num;
}
int get_digit(int dimension)
{
int answer;
int tempt = number;
int remainder;
for (size_t i = 0; i < dimension; i++)
{
tempt /= 10;
}
remainder = tempt % 10;
return remainder;
}
~my_int()=default;
private:
int number;
};
void swap(vector<int>& target,int pos_left, int pos_right)
{
int tempt = target[pos_left];
target[pos_left] = target[pos_right];
target[pos_right] = tempt;
}
int bubble_sort(vector<int>& target)
{
int count = 0;
for (int i = target.size()-1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (target[j] > target[j + 1])
{
swap(target, j, j + 1);
count++;
}
}
}
return count;
}
int choose_sort(vector<int>& target)
{
int count = 0;
int current_min = INT_MAX;
int cur_min_pos = 0;
for (size_t i = 0; i < target.size()-1; i++)
{
current_min = target[i];
cur_min_pos = i;
for (size_t j = i+1; j < target.size(); j++)
{
if (target[j] < current_min)
{
current_min = target[j];
cur_min_pos = j;
}
}
if (cur_min_pos != i)
{
swap(target, cur_min_pos, i);
count++;
}
}
return count;
}
int insert_sort(vector<int>& target)
{
int count = 0;
for (size_t i = 1; i < target.size(); i++)
{
for (int j = i; j > 0; j--)
{
if (target[j] > target[j - 1])
{
break;
}
else
{
swap(target, j, j - 1);
count++;
}
}
}
return count;
}
int shell_sort(vector<int>& target)
{
int count = 0;
int gap = target.size() / 3;
while (gap > 1)
{
for (size_t i = 0; i < gap; i++)
{
for (size_t j = i; j < target.size(); j += gap)
{
for (int k = j; k >= gap; k -= gap)
{
if (target[k] > target[k - gap])
{
break;
}
else
{
swap(target, k, k - gap);
count++;
}
}
}
}
gap = gap / 3 + 1;
}
count += insert_sort(target);
return count;
}
int my_partition(vector<int>&target, int start, int end,int& count)//count only++!!
{//return the position adjusted
int to_be_adjust = target[start];
int cut_point = start;
for (size_t i = start + 1; i < end; i++)
{
if (target[i] >= to_be_adjust)
{
;
}
else
{
cut_point++;
if (cut_point != i)
{
swap(target, cut_point, i);
count++;
}
}
}
swap(target, start, cut_point);
count++;
return cut_point;
}
void my_quick_sort(vector<int>& target, int start, int end, int& count)
{
if (end > start)
{
int cut_point = my_partition(target, start, end, count);
my_quick_sort(target, start, cut_point, count);
my_quick_sort(target, cut_point + 1, end, count);
}
}
int quick_sort(vector<int>& target)
{
int count = 0;
//partition
my_quick_sort(target, 0, target.size(),count);
//sort
//count += insert_sort(target);
return count;
}
int shitf_down(vector<int>& target, int start,int end)
{
int count = 0;
while (start < end)
{
int max_child = start * 2 + 1;
if (max_child > end)
{
break;
}
if (start * 2 + 2 < end && target[start * 2 + 2] > target[max_child])
{
max_child = start * 2 + 2;
}//choose a min one
if (target[max_child] > target[start])
{
swap(target, max_child, start);
count++;
}
start = max_child;
}
return count;
}
int heap_sort(vector<int>& target)
{
int count = 0;
for (int i = target.size() / 2; i >= 0; i--)
{
count += shitf_down(target, i, target.size());
}
for (int i = target.size() - 1; i > 0; i--)
{
swap(target, 0, i);
count++;
count += shitf_down(target, 0, i-1);
}
return count;
}
int my_merge(vector<int>& list_1, vector<int>& list_2, int left, int right)
{
int count = 0;
int mid = (left + right) / 2;
for (size_t i = left; i < right; i++)
{
list_2[i] = list_1[i];
}
int pos_1 = left;
int pos_2 = mid;
int cur_pos = left;
while (pos_1 < mid && pos_2 < right)
{
count++;
if (list_2[pos_1] < list_2[pos_2])
{
list_1[cur_pos++] = list_2[pos_1++];
}
else
{
list_1[cur_pos++] = list_2[pos_2++];
}
}
while (pos_1<mid)
{
count++;
list_1[cur_pos++] = list_2[pos_1++];
}
while (pos_2<right)
{
count++;
list_1[cur_pos++] = list_2[pos_2++];
}
return count;
}
void my_merge_sort(vector<int>& list_1, vector<int>& list_2, int left, int right,int& count)
{
if (left < right)
{
int mid = (left + right) / 2;
my_merge_sort(list_1, list_2, left, mid, count);
my_merge_sort(list_1, list_2, mid + 1, right, count);
count += my_merge(list_1, list_2, left, right);
}
}
int merge_sort(vector<int>& target)
{
int count = 0;
vector<int>list_2 = target;
my_merge_sort(target, list_2, 0, target.size(), count);
return count;
}
void LSD_sort(vector<my_int>& target, int digit)
{
vector<vector<my_int>> link_array;
link_array.resize(10);
for (size_t i = 0; i < digit; i++)// 从低位向高位 0-》n-1
{
for (size_t j = 0; j < target.size(); j++)
{
link_array[target[j].get_digit(i)].push_back(target[j]);
}
target.clear();
for (auto& dig : link_array)
{
for (auto num : dig)
{
target.push_back(num);
}
dig.clear();
}
}
}
int main()
{
cout << "** 排序算法比较 **"<< endl;
cout << "===================================================" << endl;
cout << "** 1 --- 冒泡排序 **" << endl;
cout << "** 2 --- 选择排序 **" << endl;
cout << "** 3 --- 直接插入排序 **" << endl;
cout << "** 4 --- 希尔排序 **" << endl;
cout << "** 5 --- 快速排序 **" << endl;
cout << "** 6 --- 堆排序 **" << endl;
cout << "** 7 --- 归并排序 **" << endl;
cout << "** 8 --- 基数排序 **" << endl;
cout << "** 9 --- 退出程序 **" << endl;
cout << "====================================================" << endl;
cout << endl;
cout << "请输入要产生的随机数的个数: ";
int num_rand = 0;
cin >> num_rand;
int choice;
cout << "请选择排序算法: ";
cin >> choice;
while (choice!=9)
{
switch (choice)
{
case 1:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = bubble_sort(target);
cout << "冒泡排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "冒泡排序交换次数: " << count << endl;
}break;
case 2:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = choose_sort(target);
cout << "选择排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "选择排序交换次数: " << count << endl;
}break;
case 3:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = insert_sort(target);
cout << "插入排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "插入排序交换次数: " << count << endl;
}break;
case 4:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = shell_sort(target);
cout << "希尔排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "希尔排序交换次数: " << count << endl;
}break;
case 5:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = quick_sort(target);
cout << "快速排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "快速排序交换次数: " << count << endl;
}break;
case 6:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = heap_sort(target);
cout << "堆排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "堆排序交换次数: " << count << endl;
}break;
case 7:
{
vector<int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = merge_sort(target);
cout << "归并排序所用时间: " << (clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "归并排序交换次数: " << count << endl;
}break;
case 8:
{
vector<my_int> target;
srand(static_cast<unsigned int>(time(0)));
for (size_t i = 0; i < num_rand; i++)
{
int current = rand();
target.push_back(current);
srand(static_cast<unsigned int>(current));
}
int a = RAND_MAX;
clock_t t1 = clock();
int count = 0;
LSD_sort(target, 5);
cout << "基数排序所用时间: "<<(clock() - t1) * 1.0 / CLOCKS_PER_SEC * 1000 << endl;
cout << "基数排序不存在交换的过程! " << count << endl;
}break;
default:
break;
}
cout << endl;
cout << "请选择排序算法: ";
cin >> choice;
}
return 0;
}