-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
323 lines (295 loc) · 8.58 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <ctime>
#include <random>
#include <unistd.h>
//controls speed of program
unsigned int microsecond = 1000;
//creates unsorted vector of random values
std::vector<int> vect()
{
std::random_device rd;
std::uniform_int_distribution<int> ud(1, 80 / 2);
std::vector<int> v;
for (int i = 0; i < 75; i++)
{
v.push_back(ud(rd));
}
return v;
}
//creates bar graph
void draw_state(std::vector<int> &v, std::vector<sf::RectangleShape> &bars, int red, int blue, sf::RenderWindow &window)
{
usleep(microsecond * 20);
window.clear();
for (int i = 0; i < bars.size(); i++)
{
int random_height = v[i];
sf::RectangleShape bb = bars[i];
if (i == red)
{
bb.setSize(sf::Vector2f(10.0, -random_height * 20));
bb.setFillColor(sf::Color(255, 0, 0));
}
if (i == blue)
{
bb.setSize(sf::Vector2f(10.0, -random_height * 20));
bb.setFillColor(sf::Color(0, 0, 255));
}
else
{
bb.setSize(sf::Vector2f(10.0, -random_height * 20));
bb.setFillColor(sf::Color(255, 255, 255));
}
window.draw(bb);
}
window.display();
}
//helper function for quick sort
int partition(std::vector<int> &list, int low, int high, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
int pivot = list[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++)
{
if (list[j] < pivot)
{
i++;
std::swap(list[i], list[j]);
}
draw_state(list, bars, low, high, window);
}
std::swap(list[i + 1], list[high]);
return i + 1;
}
//quick sort algorithm
void Quicksort(std::vector<int> &list, int low, int high, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
if (low < high)
{
int part = partition(list, low, high, window, bars);
Quicksort(list, low, part - 1, window, bars);
Quicksort(list, part + 1, high, window, bars);
}
}
//helper function for merge sort
void final_merge(std::vector<int> &list, std::vector<int> left, std::vector<int> right, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
int left_size = left.size();
int right_size = right.size();
int left_idx = 0, right_idx = 0, list_idx = 0;
while (left_idx < left_size && right_idx < right_size)
{
if (left[left_idx] <= right[right_idx])
{
list[list_idx] = left[left_idx];
left_idx++;
}
else
{
list[list_idx] = right[right_idx];
right_idx++;
}
draw_state(list, bars, left_idx, right_idx, window);
list_idx++;
}
while (left_idx < left_size)
{
list[list_idx] = left[left_idx];
left_idx++;
list_idx++;
}
while (right_idx < right_size)
{
list[list_idx] = right[right_idx];
right_idx++;
list_idx++;
}
}
//merge sort algorithm
void merge_sort(std::vector<int> &list, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
int list_size = list.size();
// There is only one element
if (list_size < 2)
{
return;
}
// Split elements_list in half as left and right
int middle = list_size / 2;
std::vector<int> left;
std::vector<int> right;
// Fill left and right
for (int i = 0; i < middle; i++)
{
left.push_back(list[i]);
}
for (int i = middle; i < list_size; i++)
{
right.push_back(list[i]);
}
merge_sort(left, window, bars);
merge_sort(right, window, bars);
final_merge(list, left, right, window, bars);
}
//insertion sort algorithm
void InsertionSort(std::vector<int> &v, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
for (int i = 1; i < v.size(); i++)
{
int j = i - 1;
int x = v[i];
while (j > -1 && v[j] > x)
{
v[j + 1] = v[j];
j--;
}
draw_state(v, bars, i, j, window);
v[j + 1] = x;
}
}
//selection sort algorithm
void SelectionSort(std::vector<int> &v, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
for (unsigned int i = 0; i < v.size(); i++)
{
for (unsigned int j = i; j < v.size(); j++)
{
if (v[j] < v[i])
{
std::swap(v[j], v[i]);
}
draw_state(v, bars, i, j, window);
}
}
}
//bubble sort algorithm
void BubbleSort(std::vector<int> &v, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
for (int i = 0; i < v.size() - 1; i++)
{
for (int j = 0; j < v.size() - i - 1; j++)
{
if (v[j] > v[j + 1])
{
std::swap(v[j], v[j + 1]);
}
draw_state(v, bars, i, j, window);
}
}
}
//creates bars at set length for the graph window
std::vector<sf::RectangleShape> barVector(std::vector<int> v, int height)
{
std::vector<sf::RectangleShape> bars;
for (int i = 0; i < v.size(); ++i)
{
sf::RectangleShape rect;
// shift each bar to the right
int xPos = 10 + (10 * i * 2);
int yPos = (height * .999f);
rect.setPosition(xPos, yPos);
bars.push_back(rect);
}
return bars;
}
void Initial_and_Final_Print(std::vector<int> &v, sf::RenderWindow &window, std::vector<sf::RectangleShape> &bars)
{
// DRAW RECTANGLES (Initial and Final draw states)
window.clear();
for (int i = 0; i < bars.size(); i++)
{
int random_height = v[i];
sf::RectangleShape bb = bars[i];
bb.setSize(sf::Vector2f(10.0, -random_height * 20));
bb.setFillColor(sf::Color(255, 255, 255));
window.draw(bb);
}
window.display();
}
void App()
{
int height = 1000;
int width = 1500;
// Randomized Vector of Ints
std::vector<int> v = vect();
// Vector of Bars that will be displayed
std::vector<sf::RectangleShape> bars = barVector(v, height);
// Create Window
sf::RenderWindow window(sf::VideoMode(width, height), "Sorting App");
// Track Window Events
sf::Event e;
while (window.isOpen())
{
while (window.pollEvent(e))
{
// MANUALLY CLOSE WINDOW
if (e.type == sf::Event::Closed)
{
window.close();
}
// Button Calls
if (e.type == sf::Event::KeyReleased)
{
// Hit 0 for New Vector
if (e.key.code == sf::Keyboard::Num0)
{
v = vect();
bars = barVector(v, height);
}
// Hit 1 for Bubble
if (e.key.code == sf::Keyboard::Num1)
{
BubbleSort(v, window, bars);
}
// Hit 2 to for Insertion
if (e.key.code == sf::Keyboard::Num2)
{
InsertionSort(v, window, bars);
}
// Hit 3 for Merge
if (e.key.code == sf::Keyboard::Num3)
{
merge_sort(v, window, bars);
}
// Hit 4 for Quick
if (e.key.code == sf::Keyboard::Num4)
{
Quicksort(v, 0, v.size() - 1, window, bars);
}
// Hit 5 for Selection
if (e.key.code == sf::Keyboard::Num5)
{
SelectionSort(v, window, bars);
}
}
}
Initial_and_Final_Print(v, window, bars);
}
}
int main()
{
std::cout << "There are 5 sorting algorithms you can visualize in this program." << std::endl
<< std::endl;
std::cout << "To Run:" << std::endl
<< std::endl;
std::cout << "Hit 0 for New Vector" << std::endl
<< std::endl;
std::cout << "Hit 1 for Bubble Sort" << std::endl
<< std::endl;
std::cout << "Hit 2 for Insertion Sort" << std::endl
<< std::endl;
std::cout << "Hit 3 for Merge Sort" << std::endl
<< std::endl;
std::cout << "Hit 4 for Quick Sort" << std::endl
<< std::endl;
std::cout << "Hit 5 for Selection Sort" << std::endl
<< std::endl;
std::cout << "PRESS ANY KEY TO CONTINUE..." << std::endl;
// Pause Program to allow for directional read (may need to switch "read" to "pause" for Windows users)
system("read");
// RUN APP
App();
return 0;
}