-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cpp
More file actions
282 lines (250 loc) · 9.34 KB
/
Copy pathProgram.cpp
File metadata and controls
282 lines (250 loc) · 9.34 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
#include <chrono>
#include <iostream>
#include <ncurses.h>
#include <vector>
#include "Algorithms.h"
#include <unistd.h>
typedef enum AlgTypes {
quicksort,
insertionSort,
bubbleSort,
selectionSort,
dualPivotQuicksort,
mergeSort,
bingoSort,
pancakeSort,
combSort,
gnomeSort
} AlgTypes;
typedef class Algs {
public:
AlgTypes type;
double time;
} Algs;
void ViewLeaderboard();
void SortLeaderboard();
void AddToLeaderboard(AlgTypes type, double time);
std::string GetName(AlgTypes type);
void SetupColors();
std::vector<Algs> leaderboard;
int main(int argc, char *argv[]) {
initscr();
raw();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
SetupColors();
int command = ' ';
while (command != 'Q') {
clear();
std::vector<int> arr;
for (int i = 0; i < getmaxx(stdscr); i++) {
//(rand() % (upper - lower + 1)) + lower
arr.push_back((rand() % (getmaxy(stdscr) - 1 + 1)) + 1);
}
attron(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(0, (getmaxx(stdscr) / 2) - (18 / 2), "SORTING VISUALISER");
attroff(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(4, 0, "Please type the number of the sorting method you would like to see");
mvprintw(5, 0, "or 'Q' to quit. Type 'L' to view leaderboard.");
mvprintw(6, 0, "Type 'S' to change delay settings.");
mvprintw(8, 0, "1. Quicksort");
mvprintw(9, 0, "2. Insertion Sort");
mvprintw(10, 0, "3. Bubble Sort");
mvprintw(11, 0, "4. Selection Sort");
mvprintw(12, 0, "5. Dual Pivot Quicksort");
mvprintw(13, 0, "6. Merge Sort");
mvprintw(14, 0, "7. Bingo Sort");
mvprintw(15, 0, "8. Pancake Sort");
mvprintw(16, 0, "9. Comb Sort");
mvprintw(17, 0, "X. Gnome/Stupid Sort");
std::chrono::duration<double> seconds;
mvprintw(20, 0, "Last run: %.2lf seconds.", seconds.count());
refresh();
command = getch();
if (command == 'L') {
ViewLeaderboard();
}
if (command == 'S') {
clear();
attron(COLOR_PAIR(COLOR_MAGENTA));
mvprintw(0, (getmaxx(stdscr) / 2) - (14 / 2), "DELAY SETTINGS");
attroff(COLOR_PAIR(COLOR_MAGENTA));
mvprintw(4, 0, "Pick what delay you would like to have. The lower the delay, ");
mvprintw(5, 0, "the faster the data will sort.");
mvprintw(6, 0, "(Leaderboard will clear when delay changed.)");
mvprintw(8, 0, "(1.) Very Fast (0 microseconds)");
mvprintw(9, 0, "(2.) Fast (10,000 microseconds)");
mvprintw(10, 0, "(3.) Default (20,000 microseconds)");
mvprintw(11, 0, "(4.) Slow (30,000 microseconds)");
mvprintw(12, 0, "(5.) Very Slow (40,000 microseconds)");
mvprintw(15, 0, "Current delay: %d microseconds", Algorithms::delay);
mvprintw(getmaxy(stdscr) - 1, 0, "Type q to return.");
int nextDelay = getch();
if (nextDelay == 'q')
continue;
Algorithms::delay = ((nextDelay - '0') - 1) * 10000;
leaderboard.clear();
}
if (command == '1') {
QuickSort qs;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
qs.Sort(arr, 0, (int)arr.size() - 1);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(quicksort, seconds.count());
usleep(1750000);
} else if (command == '2') {
InsertionSort is;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
is.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(insertionSort, seconds.count());
usleep(1750000);
} else if (command == '3') {
BubbleSort bs;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
bs.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(bubbleSort, seconds.count());
usleep(1750000);
} else if (command == '4') {
SelectionSort ss;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
ss.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(selectionSort, seconds.count());
usleep(1750000);
} else if (command == '5') {
DualPivotQuickSort dpqs;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
dpqs.Sort(arr, 0, (int)arr.size() - 1);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(dualPivotQuicksort, seconds.count());
usleep(1750000);
} else if (command == '6') {
MergeSort ms;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
ms.Sort(arr, 0, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(mergeSort, seconds.count());
usleep(1750000);
} else if (command == '7') {
BingoSort bs;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
bs.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(bingoSort, seconds.count());
usleep(1750000);
} else if (command == '8') {
PancakeSort ps;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
ps.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(pancakeSort, seconds.count());
usleep(1750000);
} else if (command == '9') {
CombSort cs;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
cs.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(combSort, seconds.count());
usleep(1750000);
} else if (command == 'X') {
GnomeSort gs;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
gs.Sort(arr, (int)arr.size());
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
seconds = end - start;
AddToLeaderboard(gnomeSort, seconds.count());
usleep(1750000);
}
}
endwin();
return 0;
}
void ViewLeaderboard() {
clear();
attron(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
mvprintw(0, (getmaxx(stdscr) / 2) - (11 / 2), "LEADERBOARD");
attroff(COLOR_PAIR(COLOR_MAGENTA) | A_BOLD);
SortLeaderboard();
for (int i = 0; i < (int)leaderboard.size(); i++) {
mvprintw(5 + i, 0, "%d. %s - %.2lf seconds", i + 1, GetName(leaderboard[i].type).c_str(), leaderboard[i].time);
}
mvprintw(getmaxy(stdscr) - 1, 0, "Type q to return.");
refresh();
while (getch() != 'q')
;
}
void SortLeaderboard() {
int i, j;
Algs key;
for (i = 1; i < (int)leaderboard.size(); i++) {
key = leaderboard[i];
j = i - 1;
while (j >= 0 && leaderboard[j].time > key.time) {
leaderboard[j + 1] = leaderboard[j];
j = j - 1;
}
leaderboard[j + 1] = key;
}
}
void AddToLeaderboard(AlgTypes type, double time) {
bool found = false;
for (int i = 0; i < (int)leaderboard.size(); i++) {
if (type == leaderboard[i].type) {
found = true;
if (time < leaderboard[i].time) {
leaderboard[i].time = time;
}
}
}
if (!found) {
Algs next;
next.type = type;
next.time = time;
leaderboard.push_back(next);
}
}
std::string GetName(AlgTypes type) {
switch (type) {
case 0:
return "Quicksort";
case 1:
return "Insertion Sort";
case 2:
return "Bubble Sort";
case 3:
return "Selection Sort";
case 4:
return "Dual Pivot Quicksort";
case 5:
return "Merge Sort";
case 6:
return "Bingo Sort";
case 7:
return "Pancake Sort";
case 8:
return "Comb Sort";
case 9:
return "Gnome/Stupid Sort";
default:
return "error";
}
}
void SetupColors() {
start_color();
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_BLACK, COLOR_WHITE, COLOR_BLACK);
}