-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionFive.h
436 lines (341 loc) · 10.2 KB
/
FunctionFive.h
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
#ifndef FUNCTIONFIVE_H
#define FUNCTIONFIVE_H
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;
struct UserTxt5 {
int user_id;
string user_password;
};
int ReadByUserTxt(UserTxt5 array[]) {
ifstream readIt("./OriginTxt/user.txt");
if (readIt.fail()) {
cout << "该文件没有找到!" << endl;
cout << "程序已经退出!" << endl;
exit(1);
}
int num = 0;
while (!readIt.eof()) {
readIt >> array[num].user_id >> array[num].user_password;
num++;
if (readIt.eof()) break;
}
readIt.close();
return num;
}
void WriteToTxt(UserTxt5 array[], int size, string txtName) {
ofstream writeIt;
writeIt.open(txtName);
if (writeIt.fail()) {
cout << "该文件没有找到!" << endl;
cout << "程序已经退出!" << endl;
exit(1);
}
for (int i = 0; i < size; i++) {
writeIt << array[i].user_id << "\t\t\t" << array[i].user_password << endl;
}
writeIt.close();
}
void InsertSort5(UserTxt5 array[], int size) {//从小到大排序
for (int i = 1; i < size; i++) {
UserTxt5 temp;
temp = array[i];
int j;
for (j = i; j >= 0 && (temp.user_id > array[j - 1].user_id); j--) {
array[j] = array[j - 1];
}
array[j] = temp;
}
}
void ShellSort5(UserTxt5 array[], int size) {//从小到大排序
int incrementArray[3] = { 7,3,1 };
for (int i = 0; i < 3; i++) {
int increment = incrementArray[i];
for (int j = increment; j < size; j++) {
UserTxt5 temp;
temp = array[j];
int k;
for (k = j; k >= increment; k -= increment) {
if (temp.user_id < array[k - increment].user_id) {
array[k] = array[k - increment];
}
else {
break;
}
}
array[k] = temp;
}
}
}
void BubbleSort5(UserTxt5 array[], int size) {//从小到大排序
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (array[j].user_id > array[j + 1].user_id) {
UserTxt5 temp;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
bool CompForQuickSort5(UserTxt5 x, UserTxt5 y) {
return x.user_id < y.user_id;
}
void QuickSort5(UserTxt5 array[], int size) {//从小到大排序
sort(array, array + size, CompForQuickSort5);
}
void SelectionSort5(UserTxt5 array[], int size) {//从小到大排序
for (int i = 0; i < size - 1; i++) {
int maxNum = i;
for (int j = i + 1; j < size; j++) {
if (array[j].user_id < array[maxNum].user_id) {
maxNum = j;
}
}
UserTxt5 temp;
temp = array[i];
array[i] = array[maxNum];
array[maxNum] = temp;
}
}
//堆排序开始
void DownwardAdjustment5(UserTxt5 array[], int i, int size) {//最大堆的调整
UserTxt5 temp;
temp = array[i];
int child;
for (int j = i * 2 + 1; j <= size; j = child * 2 + 1) {
child = j;
if (child != size && (array[child].user_id < array[child + 1].user_id)) {
child++;
}
if (temp.user_id < array[child].user_id) {
array[i] = array[child];
i = child;
}
else {
break;
}
}
array[i] = temp;
}
void HeapSort5(UserTxt5 array[], int size) {//从小到大排序
for (int i = (size - 1) / 2; i >= 0; i--) {
DownwardAdjustment5(array, i, size - 1);
}
for (int i = size - 1; i >= 1; i--) {
UserTxt5 temp;
temp = array[0];
array[0] = array[i];
array[i] = temp;
DownwardAdjustment5(array, 0, i - 1);
}
}
//堆排序结束
//归并排序开始
void Part5(UserTxt5 temp[], UserTxt5 array[], int low, int high);
void Merge5(UserTxt5 temp[], UserTxt5 array[], int low, int middle, int high);
void Part5(UserTxt5 temp[], UserTxt5 array[], int low, int high) {
if (low < high) {
int middle = (low + high) / 2;
Part5(temp, array, low, middle);
Part5(temp, array, middle + 1, high);
Merge5(temp, array, low, middle, high);
}
}
void Merge5(UserTxt5 temp[], UserTxt5 array[], int low, int middle, int high) {
int left = low;
int right = middle + 1;
int count = 0;
while (left <= middle && right <= high) {
if (array[left].user_id <= array[right].user_id) temp[count++] = array[left++];
else if (array[left].user_id > array[right].user_id) temp[count++] = array[right++];
}
while (left <= middle) temp[count++] = array[left++];
while (right <= high) temp[count++] = array[right++];
for (int i = 0, k = low; i < count; i++) {
array[k++] = temp[i];
}
}
void MergeSort5(UserTxt5 array[], int size) {//从小到大排序
UserTxt5* temp = new UserTxt5[2000000];
if (temp != NULL) {
Part5(temp, array, 0, size - 1);
delete[] temp;
}
else {
cout << "在 MergeSort 中内存分配失败!" << endl;
}
}
//归并排序结束
void RadixSort5(UserTxt5 array[], int size) {
int maxNum = array[0].user_id;
for (int i = 1; i < size; i++) {
if (array[i].user_id > maxNum) {
maxNum = array[i].user_id;
}
}
int count1 = 1;
UserTxt5* temp = new UserTxt5[size];
if (temp == NULL) {
cout << "在 RadixSort 中,不给分配temp临时数组空间!" << endl;
cout << "程序已停止!" << endl;
exit(1);
}
while (maxNum / count1 > 0) {
int backet[10] = { 0 };
for (int i = 0; i < size; i++) {
backet[array[i].user_id / count1 % 10]++;
}
for (int i = 1; i < 10; i++) {
backet[i] += backet[i - 1];
}
for (int i = size - 1; i >= 0; i--) {
temp[backet[array[i].user_id / count1 % 10] - 1].user_id = array[i].user_id;
temp[--backet[array[i].user_id / count1 % 10]].user_password = array[i].user_password;
}
for (int i = 0; i < size - 1; i++) {
array[i].user_id = temp[i].user_id;
array[i].user_password = temp[i].user_password;
}
count1 *= 10;
}
for (int i = 0, k = size - 1; i < size; i++) {
array[i].user_id = temp[k - 1].user_id;
array[i].user_password = temp[k - 1].user_password;
}
delete temp;
}
//每个函数的测试
void TestCaseForInsertSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
InsertSort5(array, size);
clock_t end = clock();
UserTxt5* tempArray = new UserTxt5[1500000];
for (int i = 0; i < size; i++) {
tempArray[size - i - 1] = array[i];
}
WriteToTxt(tempArray, size, "./FunctionFiveTxt/InsertSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Insert Sort need time is : " << usetime << "s" << endl;
cout << "Insert Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForShellSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
ShellSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/ShellSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Shell Sort need time is : " << usetime << "s" << endl;
cout << "Shell Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForBubbleSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
BubbleSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/BubbleSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Bubble Sort need time is : " << usetime << "s" << endl;
cout << "Bubble Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForQuickSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
QuickSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/QuickSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Quick Sort need time is : " << usetime << "s" << endl;
cout << "Quick Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForSelectionSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
SelectionSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/SelectionSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Selection Sort need time is : " << usetime << "s" << endl;
cout << "Selection Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForHeapSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
HeapSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/HeapSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Heap Sort need time is : " << usetime << "s" << endl;
cout << "Heap Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForMergeSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
MergeSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/MergeSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Merge Sort need time is : " << usetime << "s" << endl;
cout << "Merge Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
void TestCaseForRadixSort5() {
UserTxt5* array = new UserTxt5[1500000];
int size = ReadByUserTxt(array);
clock_t start = clock();
RadixSort5(array, size);
clock_t end = clock();
WriteToTxt(array, size, "./FunctionFiveTxt/RadixSort.txt");
double usetime = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl;
cout << "The function five Radix Sort need time is : " << usetime << "s" << endl;
cout << "Radix Sort Success For Function Five" << endl;
cout << endl;
delete[] array;
}
//总目录
void TestCaseForFunctionFive() {
TestCaseForQuickSort5();//
TestCaseForMergeSort5();//
TestCaseForHeapSort5();//
TestCaseForSelectionSort5();//
TestCaseForShellSort5();//
TestCaseForInsertSort5();//
TestCaseForBubbleSort5();//
//TestCaseForRadixSort5();//
}
#endif