-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksortTest.cpp
More file actions
118 lines (94 loc) · 2.47 KB
/
quicksortTest.cpp
File metadata and controls
118 lines (94 loc) · 2.47 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "testLoop.h"
int *Array;
int ArraySize;
/* Return the size of the array whose elements are smaller than pivot */
int Partition(int base, int size) {
int pivot = Array[base];
int left = base+1;
int right = base+size-1;
while(1) {
while(left<right && Array[left]<pivot)
left++;
while(right>left && Array[right]>=pivot)
right--;
if(left < right) {
int temp = Array[right];
Array[right] = Array[left];
Array[left] = temp;
left++;
right--;
}
else {
if(Array[left] >= pivot)
left--;
if(Array[right] < pivot)
right++;
int temp = Array[left];
Array[left] = pivot;
Array[base] = temp;
break;
}
}
return left-base;
}
void QuickSort(int base, int size) {
printf("QuickSort(");
for(int i=0; i<base; i++) {
printf(" ");
}
printf("[");
for(int i=0; i<size; i++) {
printf("%2d,", Array[base+i]);
}
printf("]");
for(int i=base+size; i<ArraySize; i++) {
printf(" ");
}
printf(");\n");
if(size > 1) {
int ltSize = Partition(base, size);
LOOP_BEGIN(i, 0, 2) {
QuickSort(i==0 ? base : base+ltSize+1,
i==0 ? ltSize : size-ltSize-1);
} LOOP_END;
}
}
void genTest(int *array, int size, int randomSeed) {
for(int i=0; i<size; i++) {
array[i] = i;
}
srand(randomSeed);
for(int i=0; i<size; i++) {
int des = rand()%size;
int temp = array[i];
array[i] = array[des];
array[des] = temp;
}
}
int main(int argc, char* argv[]) {
if(argc < 3) {
printf("%s <Array size> <Random seed>\n", argv[0]);
return 0;
}
ArraySize = atoi(argv[1]);
if(ArraySize > 100) {
printf("Arrays larger than 100 elements will corrupt the printing format\n");
return 0;
}
Array = (int*)malloc(sizeof(int)*ArraySize);
int seed = atoi(argv[2]);
genTest(Array, ArraySize, seed);
// This random seed determines how the execution order of QuickSort is randomized
srand(time(0));
QuickSort(0, ArraySize);
printf("Result: [");
for(int i=0; i<ArraySize; i++) {
printf("%2d,", Array[i]);
}
printf("]\n");
free(Array);
return 0;
}