-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortSuite.c
135 lines (119 loc) · 3.87 KB
/
sortSuite.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
/* Personal Collection of Sorting Algorithm Implementations
*
* Includes:
* Selection Bubble Merge Heap
* Insertion Quick Shell
*
* I plan on eventually creating an algorithm visualizor
* using either Qt or a JS webapp
*
* Usage:
*
* 1. Build: ./build
* 2. Run: ./sort <type : string> <size : int>
*
* type options: sl, in, bl, mg, qk, hp, sh
* size : integer greater than two
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <SDL2/SDL.h>
#include "sorting.h"
#include "graphics.h"
char* SORT_TYPES[7] = {"bl", "hp", "in", "mg", "qk", "sh", "sl"};
// main function
// parse execution arguments and execute call to sorting function
int main(int argc, char* argv[]) {
// check correct usage
if (argc != 3) {
printf("Usage: ./sort <type> <size>\n");
printf("Sorting Types :\n");
printf(" => bl : Bubble sort\n");
// printf(" => hp : Heap sort\n");
printf(" => in : Insertion sort\n");
printf(" => mg : Merge sort\n");
printf(" => qk : Quicksort\n");
// printf(" => sh : Shell sort\n");
printf(" => sl : Selection sort\n");
return 1;
}
// check acceptable array size range
int size = atoi(argv[2]);
if (size < 2) {
printf("Given size must be greater than 2.\n");
printf("Usage: ./sort <type> <size>\n");
return 1;
}
// check acceptable sorting type
char* sortType = argv[1];
bool accepted = false;
int type = 0;
while (type < 7 && !accepted) {
if (strcmp(SORT_TYPES[type], sortType) == 0) {
accepted = true;
}
type++;
}
if (!accepted) {
printf("Invalid Sorting Type.\n");
printf(" => bl : Bubble sort\n");
// printf(" => hp : Heap sort\n");
printf(" => in : Insertion sort\n");
printf(" => mg : Merge sort\n");
printf(" => qk : Quicksort\n");
// printf(" => sh : Shell sort\n");
printf(" => sl : Selection sort\n");
return 1;
}
// generate random array
// fills an array with random numbers from 1 to 2*SIZE
// might be better to generate an array from 1 to SIZE and then scramble
int* arr = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
int num = 1 + rand() / ( RAND_MAX / ((size) - 1 + 1) + 1);
arr[i] = num;
}
// set dimensions for rendered units
setCellWidth(getScreenWidth() / size);
setCellHeight(getScreenHeight() / (size));
setOffset((getScreenWidth() - (getCellWidth() * size)) / 2);
// initialize sdl2 graphics
init();
// draw unsorted array
outputRender(arr, size, -1);
// sdl event loop
// starts the given sorting algorithm upon keypress
bool quit = false;
bool done = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
} else if (e.type == SDL_KEYDOWN && done == false) {
if (strcmp(sortType, "bl") == 0) {
bubbleSort(arr, size);
} else if (strcmp(sortType, "in") == 0) {
insertionSort(arr, size);
} else if (strcmp(sortType, "mg") == 0) {
mergeSort(arr, size, 0, size);
} else if (strcmp(sortType, "sl") == 0) {
selectionSort(arr, size);
} else if (strcmp(sortType, "qk") == 0) {
time_t t;
srand((unsigned) time(&t));
int pivot = rand() % size;
quickSort(arr, size, 0, pivot, size);
}
done = true;
}
}
}
// close sdl2 subprocesses and free array memory
close();
free((void*)arr);
return 0;
}