-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.c
255 lines (221 loc) · 6.11 KB
/
sorting.c
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
#include "sorting.h"
#define MAXLENGTH 5000000 // maximum length of the array to be sorted
p2sf SF[] = { // sorting functions
bubble_sort,
cocktail_sort,
comb_sort,
gnome_sort,
heap_sort,
insertion_sort,
merge_sort,
quick_sort,
quick_fast_sort,
selection_sort,
shell_sort,
ciura_shell_sort
};
p2sdf SFD[] = { // sorting functions with debug
bubble_sort_debug,
cocktail_sort_debug,
comb_sort_debug,
gnome_sort_debug,
heap_sort_debug,
insertion_sort_debug,
merge_sort_debug,
quick_sort_debug,
quick_fast_sort_debug,
selection_sort_debug,
shell_sort_debug,
ciura_shell_sort_debug
};
char *SFNames[] = {
"Bubble sort",
"Cocktail sort",
"Comb sort",
"Gnome sort",
"Heap sort",
"Insertion sort",
"Merge sort",
"Quick sort",
"Quick fast sort",
"Selection sort",
"Shell sort",
"Ciura Shell sort"
};
int nSF = sizeof(SF)/sizeof(*SF); // number of sorting functions
p2sf fastSF[] = { // fast sorting functions
comb_sort,
heap_sort,
merge_sort,
quick_sort,
quick_fast_sort,
shell_sort,
ciura_shell_sort
};
p2sdf fastSFD[] = { // fast sorting functions with debug
comb_sort_debug,
heap_sort_debug,
merge_sort_debug,
quick_sort_debug,
quick_fast_sort_debug,
shell_sort_debug,
ciura_shell_sort_debug
};
char *fastSFNames[] = {
"Comb sort",
"Heap sort",
"Merge sort",
"Quick sort",
"Quick fast sort",
"Shell sort",
"Ciura Shell sort"
};
int nFastSF = sizeof(fastSF)/sizeof(*fastSF); // number of fast sorting functions
int main() {
int maxlen = 500000, len; // array length
data originalArray[maxlen]; // original set of data
// Decreasing
len = 5000;
printf("ARRAY LENGTH: %d. TYPE: Decreasing\n", len);
decreasing_array(originalArray, len);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
len = 15000;
printf("ARRAY LENGTH: %d. TYPE: Decreasing\n", len);
decreasing_array(originalArray, len);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
len = 25000;
printf("ARRAY LENGTH: %d. TYPE: Decreasing\n", len);
decreasing_array(originalArray, len);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
// Almost sorted
len = 5000;
printf("ARRAY LENGTH: %d. TYPE: Almost sorted\n", len);
almost_sorted_array(originalArray, len, 1000, 100);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
len = 15000;
printf("ARRAY LENGTH: %d. TYPE: Almost sorted\n", len);
almost_sorted_array(originalArray, len, 3000, 300);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
len = 25000;
printf("ARRAY LENGTH: %d. TYPE: Almost sorted\n", len);
almost_sorted_array(originalArray, len, 5000, 500);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
// Random
random_array(originalArray, maxlen); // generate random array
len = 5000;
printf("ARRAY LENGTH: %d. TYPE: Random\n", len);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
len = 25000;
printf("ARRAY LENGTH: %d. TYPE: Random\n", len);
execute_test(originalArray, len, SF, SFD, SFNames, nSF);
len = 50000;
printf("ARRAY LENGTH: %d. TYPE: Random\n", len);
execute_test(originalArray, len, fastSF, fastSFD, fastSFNames, nFastSF);
len = 100000;
printf("ARRAY LENGTH: %d. TYPE: Random\n", len);
execute_test(originalArray, len, fastSF, fastSFD, fastSFNames, nFastSF);
len = 300000;
printf("ARRAY LENGTH: %d. TYPE: Random\n", len);
execute_test(originalArray, len, fastSF, fastSFD, fastSFNames, nFastSF);
len = maxlen;
printf("ARRAY LENGTH: %d. TYPE: Random\n", len);
execute_test(originalArray, len, fastSF, fastSFD, fastSFNames, nFastSF);
}
void reset_stat(STAT *stat) {
stat->comp = 0;
stat->swap = 0;
stat->recc = 0;
stat->read = 0;
stat->write = 0;
stat->allo = 0;
stat->space = 0;
stat->cusp = 0;
}
void print_stat(STAT stat) {
printf("%15d%15d%15d%15d%15d%15d\n", stat.comp, stat.swap, stat.write, stat.recc, stat.space, stat.allo);
}
void random_array(data array[], int len) {
int i;
for (i = 0; i < len; i++)
array[i] = rand();
}
void decreasing_array(data array[], int len) {
int i;
for (i = 0; i < len; i++)
array[i] = len-i;
}
void almost_sorted_array(data array[], int len, int n, int d) {
int i, j, k;
data tmp;
for (i = 0; i < len; i++)
array[i] = i;
for (i = 0; i < n; i++) {
j = rand() % (len-d);
k = j + (rand() % d);
tmp = array[j];
array[j] = array[k];
array[k] = tmp;
}
}
void copy_array(data a[], data b[], int len) {
int i;
for (i = 0; i < len; i++)
b[i] = a[i];
}
int compare_arrays(data a[], data b[], int len) {
int i;
for (i = 0; i < len; i++)
if (a[i] != b[i])
return 0;
return 1;
}
int check_sorted(data array[], int len) {
int i;
for (i = 0; i < len-1; i++)
if (array[i] > array[i+1])
return 0;
return 1;
}
void print_array(data array[], int len) {
int i;
for (i = 0; i < len-1; i++)
printf("%d - ", array[i]);
printf("%d\n", array[len-1]);
}
int execute_test(data originalArray[], int len, p2sf SF[], p2sdf SFD[], char *SFNames[], int nSF) {
data sortedArray[len]; // already sorted array used to check sorting
data array[len]; // array to be sorted
STAT stat;
clock_t start, stop, elapsed;
int i; // index used to select the sorting function
copy_array(originalArray, sortedArray, len); // copy to sortedArray
quick_sort(sortedArray, len); // sort the sortedArray
if (check_sorted(sortedArray, len))
printf("Initialization completed successfully.\n");
else {
printf("Initialization error.\n");
return 0;
}
printf("SORTING FUNCTION TIME COMPARISONS SWAPPINGS WRITES RECURSION SPACE ALLOCATIONS\n");
for (i = 0; i < nSF; i++) {
copy_array(originalArray, array, len); // build array
reset_stat(&stat); // reset stat
(*SFD[i])(array, len, &stat); // debugging test
if (!compare_arrays(array, sortedArray, len)) { // if sorting error
printf("%-20s Sorting failed.\n", SFNames[i]);
continue;
}
copy_array(originalArray, array, len); // build array
start = clock();
(*SF[i])(array, len); // timing test
stop = clock();
if (!compare_arrays(array, sortedArray, len)) { // if sorting error
printf("%-20s Sorting failed.\n", SFNames[i]);
continue;
}
printf("%-20s %2.2f", SFNames[i], (float)(stop-start)/CLOCKS_PER_SEC);
print_stat(stat);
}
printf("\n");
return 1;
}