-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortAlgorithm.cpp
More file actions
577 lines (521 loc) · 15.5 KB
/
SortAlgorithm.cpp
File metadata and controls
577 lines (521 loc) · 15.5 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//https://magiclen.org/sorting-algorithm/
#include <iostream>
#define IS_SHOW_LOG false
/*
Function aim : export Log file
parameter->
FuncName : What does word want to export
*/
void ShowLog(char * FuncName) {
std::cout << FuncName << std::endl;
}
/*
Function aim : Initial location of the sort array
parameter->
SortLocArr : Declared memory block of the sort array which has not been initialized;
SortNum : How many sizes are in the array;
*/
void InitSortLocArr(int * SortLocArr, int SortNum) {
if (IS_SHOW_LOG == true) {
ShowLog("InitSortLocArr");
}
for (int i = 0; i < SortNum; i++) {
SortLocArr[i] = i;
}
}
/*
Function aim : It is to Swap the value between memory address of A and B
*/
void Swap(int *A , int *B) {
if (IS_SHOW_LOG == true) {
ShowLog("Swap");
}
int tmp = *A;
*A = *B;
*B = tmp;
}
/*
Function aim : Is x smaller than y if it is true then the function must return the value of x.
Otherwise, the function must return the value of y.
*/
int Min(int x, int y) {
return (x < y) ? x : y;
}
/*
Function aim : It is implement the Bubble Sort Algorithm.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void BubbleSort(int * NonSortArr, int SortNum,int * SortLocArr)
{
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
for (int i = SortNum - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (tNonSortArr[j] > tNonSortArr[j + 1]) {
Swap(&tNonSortArr[j], &tNonSortArr[j + 1]);
Swap(&SortLocArr[j], &SortLocArr[j + 1]);
}
}
}
}
/*
Function aim : It is implement the Exchange Sort Algorithm.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void ExchangeSort(int * NonSortArr, int SortNum, int * SortLocArr) {
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
for (int i = 0; i < SortNum - 1 ; i++){
for (int j = i + 1; j < SortNum; j++) {
if (tNonSortArr[i] > tNonSortArr[j]) {
Swap(&tNonSortArr[i], &tNonSortArr[j]);
Swap(&SortLocArr[i], &SortLocArr[j]);
}
}
}
}
/*
Function aim : It is implement the Selection Sort Algorithm.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void SelectionSort(int * NonSortArr, int SortNum, int * SortLocArr) {
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
for (int i = 0; i < SortNum - 1; i++) {
for (int j = i + 1; j < SortNum; j++) {
int k = i;
if (tNonSortArr[k] > tNonSortArr[j]) {
k = j;
}
if (k != i) {
Swap(&tNonSortArr[i], &tNonSortArr[j]);
Swap(&SortLocArr[i], &SortLocArr[j]);
}
}
}
}
/*
Function aim : It is implement the Insertion Sort Algorithm.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void InsertionSort(int * NonSortArr, int SortNum, int * SortLocArr) {
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
for (int i = 0; i < SortNum; i++) {
int tTmp = tNonSortArr[i];
int tLoc = SortLocArr[i];
int j = i - 1;
while( j >= 0 && tNonSortArr[j] > tTmp ) {
tNonSortArr[j + 1] = tNonSortArr[j];
SortLocArr[j + 1] = SortLocArr[j];
j--;
}
tNonSortArr[j + 1] = tTmp;
SortLocArr[j + 1] = tLoc;
}
}
int Partition(int number[], int left, int right, int *SortLocArr) {
int i = left - 1;
int j;
for (j = left; j < right; j++) {
if (number[j] <= number[right]) {
i++;
Swap(&number[i], &number[j]);
Swap(&SortLocArr[i], &SortLocArr[j]);
}
}
Swap(&number[i + 1], &number[right]);
Swap(&SortLocArr[i + 1], &SortLocArr[right]);
return i + 1;
}
/* A[] --> Array to be sorted,
l --> Starting index,
h --> Ending index */
void QuickSortRecursiveFunc(int data[], int left, int right, int *SortLocArr)
{
if (left < right) {
int q = Partition(data, left, right, SortLocArr);
QuickSortRecursiveFunc(data, left, q - 1, SortLocArr);
QuickSortRecursiveFunc(data, q + 1, right, SortLocArr);
}
}
/*
Function aim : It is implement the quick sort algorithm by recursive way.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void QuickSortRecursive(int * NonSortArr, int SortNum, int * SortLocArr) {
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
QuickSortRecursiveFunc(tNonSortArr, 0, SortNum- 1, SortLocArr);
}
/* This function is same in both iterative and recursive*/
int partition(int* A, int low, int high, int * SortLocArr)
{
int mid = (low + high) / 2;
int pivot = A[mid];
while (low <= high) {
while (low <= high && A[high] >= pivot) {
high--;
}
while (low <= high && A[low] <= pivot) {
low++;
}
if (low <= high) {
Swap(&SortLocArr[low], &SortLocArr[high]);
Swap(&A[low], &A[high]);
high--;
low++;
}
}
return mid;
}
/* A[] --> Array to be sorted,
l --> Starting index,
h --> Ending index */
void QuickSortIterativeFunc(int arr[], int l, int h, int * SortLocArr)
{
// Create an auxiliary stack
int * stack = new int[h - l + 1];
// initialize top of stack
int top = -1;
// push initial values of l and h to stack
stack[++top] = l;
stack[++top] = h;
// Keep popping from stack while is not empty
while (top >= 0)
{
// Pop h and l
h = stack[top--];
l = stack[top--];
// Set pivot element at its correct position
// in sorted array
int p = partition(arr, l, h, SortLocArr);
// If there are elements on left side of pivot,
// then push left side to stack
if (p - 1 > l)
{
stack[++top] = l;
stack[++top] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if (p + 1 < h)
{
stack[++top] = p + 1;
stack[++top] = h;
}
}
delete[] stack;
}
/*
Function aim : It is implement the quick sort algorithm by Itertive way.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void QuickSortIterative(int * NonSortArr, int SortNum, int * SortLocArr) {
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
//QuickSortIterativeFunc(tNonSortArray, 0, SortNumber - 1, SortLocArray);
int startIndex = 0;
int endIndex = SortNum - 1;
int top = -1;
int* stack = (int*)malloc(sizeof(int) * SortNum);
stack[++top] = startIndex;
stack[++top] = endIndex;
while (top >= 0)
{
endIndex = stack[top--];
startIndex = stack[top--];
int p = Partition(tNonSortArr, startIndex, endIndex, SortLocArr);
if (p - 1 > startIndex)
{
stack[++top] = startIndex;
stack[++top] = p - 1;
}
if (p + 1 < endIndex)
{
stack[++top] = p + 1;
stack[++top] = endIndex;
}
}
delete [] stack;
}
void MergingSortR(int arr[], int l, int m, int r, int * SortLocArr)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int * L = new int[n1];
int * R = new int[n2];
int * LocL = new int[n1];
int * LocR = new int[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++) {
L[i] = arr[l + i];
LocL[i] = SortLocArr[l + i];
}
for (j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
LocR[j] = SortLocArr[m + 1 + j];
}
/* Merge the temp arrays back into arr[l..r]*/
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
SortLocArr[k] = LocL[i];
i++;
}
else
{
arr[k] = R[j];
SortLocArr[k] = LocR[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there are any */
while (i < n1)
{
arr[k] = L[i];
SortLocArr[k] = LocL[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there are any */
while (j < n2)
{
arr[k] = R[j];
SortLocArr[k] = LocR[j];
j++;
k++;
}
delete[] L;
delete[] R;
delete[] LocL;
delete[] LocR;
}
void MergeSortRecursiveFunc(int * arr,int l , int r, int * SortLocArr) {
if (l < r)
{
int m = l + (r - l) / 2; //Same as (l+r)/2 but avoids overflow for large l & h
MergeSortRecursiveFunc(arr, l, m, SortLocArr);
MergeSortRecursiveFunc(arr, m + 1, r, SortLocArr);
MergingSortR(arr, l, m, r, SortLocArr);
}
}
/*
Function aim : It is implement the merge sort algorithm by recursive way.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void MergeSortRecursive(int * NonSortArr, int SortNum, int * SortLocArr) {
int * tNonSortArr = new int[SortNum];
::memcpy_s(tNonSortArr, sizeof(int) * SortNum, NonSortArr, sizeof(int) * SortNum);
InitSortLocArr(SortLocArr, SortNum);
MergeSortRecursiveFunc(tNonSortArr, 0, SortNum - 1, SortLocArr);
}
/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
void MergeSortIterativeFunc(int *NonSortArr, int left_start, int mid, int right_end, int * SortLocArr)
{
int i, j, k;
int n1 = mid - left_start + 1;
int n2 = right_end - mid;
/* create temp arrays */
int *L = new int[n1];
int *R = new int[n2];
int *LocL = new int[n1];
int *LocR = new int[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++) {
L[i] = NonSortArr[left_start + i];
LocL[i] = SortLocArr[left_start + i];
}
for (j = 0; j < n2; j++)
{
R[j] = NonSortArr[mid + 1 + j];
LocR[j] = SortLocArr[mid + 1 + j];
}
/* Merge the temp arrays back into arr[l..r]*/
i = 0;
j = 0;
k = left_start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
NonSortArr[k] = L[i];
SortLocArr[k] = LocL[i];
i++;
}
else
{
NonSortArr[k] = R[j];
SortLocArr[k] = LocR[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there are any */
while (i < n1)
{
NonSortArr[k] = L[i];
SortLocArr[k] = LocL[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there are any */
while (j < n2)
{
NonSortArr[k] = R[j];
SortLocArr[k] = LocR[j];
j++;
k++;
}
delete[] L;
delete[] R;
delete[] LocL;
delete[] LocR;
}
/*
Function aim : It is implement the merge sort algorithm by iterative way.
Parameter->
NonSortArr(Input) : It is the original array that isn't sorted;
SortNum(Input) : How many sizes are in the array
SortLocArr(Output) : It will be the sort result after this algorithm finish
*/
void MergeSortIterative(int* NonSortArr, int SortNum, int * SortLocArr)
{
//Copy Non-Sort Array to temporay memory
int * tNonSortArr = new int[SortNum];// = new int[SortNumber];
::memcpy_s(tNonSortArr, sizeof(int)*SortNum, NonSortArr, sizeof(int)*SortNum);
//Initial Sort Location array
InitSortLocArr(SortLocArr, SortNum);
int curr_size; // For current size of subarrays to be merged
// curr_size varies from 1 to n/2
int left_start; // For picking starting index of left subarray to be merged
// Merge subarrays in bottom up manner. First merge subarrays of
// size 1 to create sorted subarrays of size 2, then merge subarrays
// of size 2 to create sorted subarrays of size 4, and so on.
for (curr_size = 1; curr_size <= SortNum - 1; curr_size = 2 * curr_size)
{
// Pick starting point of different subarrays of current size
for (left_start = 0; left_start < SortNum - 1; left_start += 2 * curr_size)
{
// Find ending point of left subarray. mid+1 is starting point of right
int mid = left_start + curr_size - 1;
int right_end = Min(left_start + 2 * curr_size - 1, SortNum - 1);
if (mid > right_end) {
mid = right_end;
}
// Merge Subarrays arr[left_start...mid] & arr[mid+1...right_end]
MergeSortIterativeFunc(tNonSortArr, left_start, mid, right_end, SortLocArr);
}
}
}
#include <random>
#include <time.h>
#define TESTRUN 1000000
/* Generate random numbers in the half-closed interval
[range_min, range_max). In other words,
range_min <= random number < range_max*/
double RandGen(int MaxValue, int MinValue) {
return (double)rand() / (RAND_MAX + 1) * (MaxValue - MinValue) + MinValue;
}
void ShowSortedArr(int * nonSortArr, int SortNum, int * SortLocArr) {
for (int i = 0; i < SortNum; i++) {
std::cout << SortLocArr[i] << " ";
}
std::cout << std::endl;
}
void main() {
int tTestRun = 0;
srand(time(NULL)); //Initialize the start value of the random tool
while (tTestRun < TESTRUN) {
tTestRun++;
const int tRandNum = RandGen(100, 2); //How many size in the non sort array
int * t_TestArray = new int[tRandNum] ;
int * t_TestSortArray = new int[tRandNum];
std::cout << "..........Problem Run " << tTestRun << ".........." << std::endl;
/*
Generate random value to set the array which you want to be sorted
*/
for (int i = 0; i < tRandNum; i++) {
t_TestArray[i] = RandGen(10000, -50000);
std::cout << t_TestArray[i] << " ";
}
std::cout << std::endl;
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Bublle Sort Algorithm" << std::endl;
BubbleSort(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Exchange Sort Algorithm" << std::endl;
ExchangeSort(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Selection Sort Algorithm" << std::endl;
SelectionSort(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Insertion Sort Algorithm" << std::endl;
InsertionSort(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Quick Sort Algorithm by Recursive way" << std::endl;
QuickSortRecursive(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Quick Sort Algorithm by Iterative way" << std::endl;
QuickSortIterative(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Merge Sort Algorithm by Recursive way" << std::endl;
MergeSortRecursive(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
std::cout << "Merge Sort Algorithm by Iterative way" << std::endl;
MergeSortIterative(t_TestArray, tRandNum, t_TestSortArray);
ShowSortedArr(t_TestArray, tRandNum, t_TestSortArray);
std::cout << "----------" << std::endl;
//std::system("pause");
}
}