-
Notifications
You must be signed in to change notification settings - Fork 0
/
c-sorting-algorithms.c
391 lines (364 loc) · 11.7 KB
/
c-sorting-algorithms.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //Include time.h library to measure running time
void table_filling(int *x, int y); //Fill the table pinakas_random with random values
void insertion_asc(int *x, int y); //Insertion sort-Ascending sort function
void insertion_desc(int *x, int y); //Insertion sort-Descending sort function
void selection_asc(int *x, int y); //Selection sort-Ascending sort function
void selection_desc(int *x, int y); //Selection sort-Descending sort function
void quick_asc(int *x, int m, int y); //Quick sort-Ascending sort function
void quick_desc(int *x, int m, int y); //Quick sort-Descending sort function
void alltasks(int *x, int *y, int *z, int j); //Run all functions
void copy(int *x, int *y, int *z, int j); //Copy values from table pinakas_random to tables pinakas_asc and pinakas_desc
void printlist(int *x, char *y, int n, double c, int t); //Print only 10 sorted values. To see all sorted values, delete second if.
void swap(int *x, int *y); //Swap values
int choose_pivot(int i, int j); //Choose pivot of values i and j
int main(int argc, char *argv[])
{
clock_t start2, end2, start3, end3, start4, end4, start5, end5, start6, end6, start7, end7; //Declaration of start and end clock
double c2, c3, c4, c5, c6, c7;
int x = 0, n = 0, i;
char *s; //Put the name of every sorting function
while (n == 0)
{
printf("Give the number of integers N:");
scanf("%d", &n);
if (n <= 0)
{
printf("Wrong value!\n");
n = 0;
}
} //Get value of n>0
int *pinakas_random = malloc(sizeof(int) * n); //Declaration of pinakas_random size
int *pinakas_asc = malloc(sizeof(int) * n); //Declaration of pinakas_asc size
int *pinakas_desc = malloc(sizeof(int) * n); //Declaration of pinakas_desc size
while (x != 9)
{
printf("--------------------------------------------------------------------------------\n");
printf("Choose one of the following tasks every time!\n\n");
printf("1. Fill the table with random integers.\n2. Ascending sort (Insertion Sort).\n3. Ascending sort (Selection Sort).\n4. Ascending sort (Quick Sort).\n5. Descending sort (Insertion Sort).\n6. Descending sort (Selection Sort).\n7. Descending sort (Quick Sort).\n8. Execute all tasks.\n9. Exit.\n\n");
printf("Give task:");
scanf("%d", &x); //Scan the number of task
switch (x)
{
case 1:
table_filling(pinakas_random, n); //Fill the pinakas_random with random values
printf("\n1. Pinakas_random fill with the values");
for (i = 0; i < n; i++)
printf(",%d", pinakas_random[i]);
printf("\n\n");
break;
case 2:
start2 = clock(); //Clock 2 starts here
copy(pinakas_random, pinakas_asc, pinakas_desc, n); //Initialise tables pinakas_asc and pinakas_desc
printf("2. Ascending sort-Insertion Sort(before sorting");
for (i = 0; i < n; i++)
printf(",%d", pinakas_asc[i]);
printf(").\n\n");
insertion_asc(pinakas_asc, n); //Sort the list
end2 = clock(); //Clock 2 ends here
c2 = ((double)(end2 - start2)) / CLOCKS_PER_SEC;
s = "Insertion Sort";
printlist(pinakas_asc, s, n, c2, 1);
break;
case 3:
start3 = clock(); //Clock 3 starts here
copy(pinakas_random, pinakas_asc, pinakas_desc, n); //Initialise tables pinakas_asc and pinakas_desc
printf("3. Ascending sort-Selection Sort(before sorting");
for (i = 0; i < n; i++)
printf(",%d", pinakas_asc[i]);
printf(").\n\n");
selection_asc(pinakas_asc, n); //Sort the list
end3 = clock(); //Clock 3 ends here
c3 = ((double)(end3 - start3)) / CLOCKS_PER_SEC;
s = "Selection Sort";
printlist(pinakas_asc, s, n, c3, 1);
break;
case 4:
start4 = clock(); //Clock 4 starts here
copy(pinakas_random, pinakas_asc, pinakas_desc, n); //Initialise tables pinakas_asc and pinakas_desc
printf("4. Ascending sort-Quick Sort(before sorting");
for (i = 0; i < n; i++)
printf(",%d", pinakas_asc[i]);
printf(").\n\n");
quick_asc(pinakas_asc, 0, n - 1); //Sort the list
end4 = clock(); //Clock 4 ends here
c4 = ((double)(end4 - start4)) / CLOCKS_PER_SEC;
s = "Quick Sort ";
printlist(pinakas_asc, s, n, c4, 1);
break;
case 5:
start5 = clock(); //Clock 5 starts here
copy(pinakas_random, pinakas_asc, pinakas_desc, n); //Initialise tables pinakas_asc and pinakas_desc
printf("5. Descending sort-Insertion Sort(before sorting");
for (i = 0; i < n; i++)
printf(",%d", pinakas_desc[i]);
printf(").\n\n");
insertion_desc(pinakas_desc, n); //Sort the list
end5 = clock(); //Clock 5 ends here
c5 = ((double)(end5 - start5)) / CLOCKS_PER_SEC;
s = "Insertion Sort";
printlist(pinakas_desc, s, n, c5, 2);
break;
case 6:
start6 = clock(); //Clock 6 starts here
copy(pinakas_random, pinakas_asc, pinakas_desc, n); //Initialise tables pinakas_asc and pinakas_desc
printf("6. Descending sort-Selection Sort(before sorting:");
for (i = 0; i < n; i++)
printf(",%d", pinakas_desc[i]);
printf(").\n\n");
selection_desc(pinakas_desc, n); //Sort the list
end6 = clock(); //Clock 6 ends here
c6 = ((double)(end6 - start6)) / CLOCKS_PER_SEC;
s = "Selection Sort";
printlist(pinakas_desc, s, n, c6, 2);
break;
case 7:
start7 = clock(); //Clock 7 starts here
copy(pinakas_random, pinakas_asc, pinakas_desc, n); //Initialise tables pinakas_asc and pinakas_desc
printf("7. Descending sort-Quick Sort(before sorting");
for (i = 0; i < n; i++)
printf(",%d", pinakas_desc[i]);
printf(").\n\n");
quick_desc(pinakas_desc, 0, n - 1); //Sort the list
end7 = clock(); //Clock 7 ends here
c7 = ((double)(end7 - start7)) / CLOCKS_PER_SEC;
s = "Quick Sort ";
printlist(pinakas_desc, s, n, c7, 2);
break;
case 8:
printf("8. Execute all tasks(before sorting");
for (i = 0; i < n; i++)
printf(",%d", pinakas_random[i]);
printf(").\n\n");
alltasks(pinakas_random, pinakas_asc, pinakas_desc, n); //Run all sorting functions
break;
case 9:
printf("Thank you... Close window of running program.\n");
break;
default:
printf("Your choice does not exist.\n");
printf("\n\n");
} //End switch
} //End while
system("PAUSE");
return 0;
}
void table_filling(int *x, int y)
{
int i;
for (i = 0; i < y; i++)
x[i] = rand() % 100;
}
void copy(int *x, int *y, int *z, int j)
{
int i;
for (i = 0; i < j; i++)
{
y[i] = x[i];
z[i] = x[i];
}
}
void insertion_asc(int a[], int n)
{
int i, j, x, k;
for (i = 1; i <= n - 1; i++)
{
j = i;
x = a[i];
while (a[j - 1] > x && j > 0)
{
a[j] = a[j - 1];
j = j - 1;
}
a[j] = x;
//printf("\nThe array after pass no.%d: ",i); Remove '//' to see steps of sorting
for (k = 0; k <= n - 1; k++)
;
//printf("%4d",a[k]); Remove '//' to see steps of sorting
}
}
void insertion_desc(int a[], int n)
{
int i, j, x, k;
for (i = 1; i <= n - 1; i++)
{
j = i;
x = a[i];
while (a[j - 1] < x && j > 0)
{
a[j] = a[j - 1];
j = j - 1;
}
a[j] = x;
//printf("\nThe array after pass no.%d: ",i); Remove '//' to see steps of sorting
for (k = 0; k <= n - 1; k++)
;
//printf("%4d",a[k]); Remove '//' to see steps of sorting
}
}
void selection_asc(int list[], int n)
{
int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (list[j] < list[min])
{
min = j;
}
}
swap(&list[i], &list[min]);
}
}
void selection_desc(int list[], int n)
{
int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (list[j] > list[min])
{
min = j;
}
}
swap(&list[i], &list[min]);
}
}
void quick_asc(int list[], int m, int n)
{
int key, i, j, k;
if (m < n)
{
k = choose_pivot(m, n);
swap(&list[m], &list[k]);
key = list[m];
i = m + 1;
j = n;
while (i <= j)
{
while ((i <= n) && (list[i] <= key))
i++;
while ((j >= m) && (list[j] > key))
j--;
if (i < j)
swap(&list[i], &list[j]);
}
// swap two elements
swap(&list[m], &list[j]);
// recursively sort the lesser list
quick_asc(list, m, j - 1);
quick_asc(list, j + 1, n);
}
}
void quick_desc(int list[], int m, int n)
{
int key, i, j, k;
if (m < n)
{
k = choose_pivot(m, n);
swap(&list[m], &list[k]);
key = list[m];
i = m + 1;
j = n;
while (i <= j)
{
while ((i <= n) && (list[i] >= key))
i++;
while ((j >= m) && (list[j] < key))
j--;
if (i < j)
swap(&list[i], &list[j]);
}
// swap two elements
swap(&list[m], &list[j]);
// recursively sort the lesser list
quick_desc(list, m, j - 1);
quick_desc(list, j + 1, n);
}
}
void alltasks(int *x, int *y, int *z, int j)
{
clock_t start2, end2, start3, end3, start4, end4, start5, end5, start6, end6, start7, end7;
double c2, c3, c4, c5, c6, c7;
char *s;
start2 = clock();
copy(x, y, z, j);
insertion_asc(y, j);
s = "Insertion Sort";
end2 = clock();
c2 = ((double)(end2 - start2)) / CLOCKS_PER_SEC;
printlist(y, s, j, c2, 1);
start3 = clock();
copy(x, y, z, j);
selection_asc(y, j);
s = "Selection Sort";
end3 = clock();
c3 = ((double)(end3 - start3)) / CLOCKS_PER_SEC;
printlist(y, s, j, c3, 1);
start4 = clock();
copy(x, y, z, j);
quick_asc(y, 0, j - 1);
s = "Quick Sort ";
end4 = clock();
c4 = ((double)(end4 - start4)) / CLOCKS_PER_SEC;
printlist(y, s, j, c4, 1);
start5 = clock();
copy(x, y, z, j);
insertion_desc(z, j);
s = "Insertion Sort";
end5 = clock();
c5 = ((double)(end5 - start5)) / CLOCKS_PER_SEC;
printlist(z, s, j, c5, 2);
start6 = clock();
copy(x, y, z, j);
selection_desc(z, j);
s = "Selection Sort";
end6 = clock();
c6 = ((double)(end6 - start6)) / CLOCKS_PER_SEC;
printlist(z, s, j, c6, 2);
start7 = clock();
copy(x, y, z, j);
quick_desc(z, 0, j - 1);
s = "Quick Sort ";
end7 = clock();
c7 = ((double)(end7 - start7)) / CLOCKS_PER_SEC;
printlist(z, s, j, c7, 2);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void printlist(int *x, char *y, int n, double c, int t)
{
char *p;
if (t == 1)
p = "pinakas_asc";
else
p = "pinakas_desc";
int i;
printf(" N Algorithm Table Values Time\n");
printf("%d %s %s ", n, y, p);
for (i = 0; i < n; i++)
{
if (i == 0)
printf("%d", x[i]);
else
{
if (i < 10)
printf(",%d", x[i]); //Print only the first sorted values. To see all values delete if
}
}
printf(" %.0fms\n\n", c * 1000);
}
int choose_pivot(int i, int j)
{
return ((i + j) / 2);
}