-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
247 lines (210 loc) · 7.9 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
#include <iostream>
#include <SFML/Graphics.hpp>
void shuffleArray(int maxNumber, int *foo, int arrSize, int &start, int &end, int &top, int &pos, int &iteration) {
start = 0;
end = arrSize - 1;
top = -1;
pos = arrSize - 1;
iteration = 0;
for (int i = 0; i < arrSize; ++i) {
foo[i] = rand() % maxNumber;
}
}
void insertionSort(int foo[], int arrSize, int &pos) {
int indexNew = pos - 1;
int valueNew = foo[indexNew];
while ((indexNew < arrSize - 1) && (valueNew > foo[indexNew + 1])) {
foo[indexNew] = foo[indexNew + 1];
indexNew++;
}
foo[indexNew] = valueNew;
pos--;
}
void swap(int *value1, int *value2) {
int temp = *value1;
*value1 = *value2;
*value2 = temp;
}
int partition(int foo[], int start, int end) {
int pivot = foo[end];
int i = (start - 1);
for (int j = start; j <= end - 1; j++) {
if (foo[j] < pivot) {
i++;
swap(&foo[i], &foo[j]);
}
}
swap(&foo[i + 1], &foo[end]);
return i + 1;
}
void quicksort(int foo[], int &start, int &end, int &top, int stack[]) {
if (top >= 0) {
// Pop end and start
end = stack[top--];
start = stack[top--];
// Set pivot element at its correct position
// in sorted array
int p = partition(foo, start, end);
// If there are elements on left side of pivot,
// then push left side to stack
if (p - 1 > start) {
stack[++top] = start;
stack[++top] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if (p + 1 < end) {
stack[++top] = p + 1;
stack[++top] = end;
}
}
}
void bubbleSort(int foo[], int arrSize, int &iteration) {
// Last i elements are already in place
for (int j = 0; j < arrSize - iteration - 1; j++) {
if (foo[j] > foo[j + 1]) {
swap(&foo[j], &foo[j + 1]);
}
}
iteration++;
}
void qSort(int windowWidth, int windowHeight, int maxNumber, int *foo, int arrSize, int *stack,
sf::RenderWindow &window, int &start, int &end, int &top) {
float width = ((float) windowWidth / (float) arrSize);
float offset = 0;
if (top != -1) {
quicksort(foo, start, end, top, stack);
}
std::vector<sf::RectangleShape> rectangles(arrSize);
for (int i = 0; i < rectangles.size(); ++i) {
float height = (float) windowHeight * ((float) foo[i] / (float) maxNumber);
rectangles[i] = sf::RectangleShape(sf::Vector2f(width, height));
rectangles[i].setOrigin(0, 0);
rectangles[i].move(offset, (float) windowHeight - height);
rectangles[i].setFillColor(sf::Color(255, 255, 255));
offset += width;
window.draw(rectangles[i]);
}
}
void iSort(int windowWidth, int windowHeight, int maxNumber, int *foo, int arrSize,
sf::RenderWindow &window, int &pos) {
float width = ((float) windowWidth / (float) arrSize);
float offset = 0;
if (pos > -1) {
insertionSort(foo, arrSize, pos);
}
std::vector<sf::RectangleShape> rectangles(arrSize);
for (int i = 0; i < rectangles.size(); ++i) {
float height = (float) windowHeight * ((float) foo[i] / (float) maxNumber);
rectangles[i] = sf::RectangleShape(sf::Vector2f(width, height));
rectangles[i].setOrigin(0, 0);
rectangles[i].move(offset, (float) windowHeight - height);
rectangles[i].setFillColor(sf::Color(255, 255, 255));
offset += width;
window.draw(rectangles[i]);
}
}
void bSort(int windowWidth, int windowHeight, int maxNumber, int *foo, int arrSize,
sf::RenderWindow &window, int &iteration) {
float width = ((float) windowWidth / (float) arrSize);
float offset = 0;
bubbleSort(foo, arrSize, iteration);
std::vector<sf::RectangleShape> rectangles(arrSize);
for (int i = 0; i < rectangles.size(); ++i) {
float height = (float) windowHeight * ((float) foo[i] / (float) maxNumber);
rectangles[i] = sf::RectangleShape(sf::Vector2f(width, height));
rectangles[i].setOrigin(0, 0);
rectangles[i].move(offset, (float) windowHeight - height);
rectangles[i].setFillColor(sf::Color(255, 255, 255));
offset += width;
window.draw(rectangles[i]);
}
}
int main() {
int windowWidth = 1500;
int windowHeight = 1000;
int maxNumber = 1000;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Sorting Algorithms");
int foo[2000];
int arrSize = sizeof(foo) / sizeof(foo[0]);
int start, end, top, pos, iteration;
shuffleArray(maxNumber, foo, arrSize, start, end, top, pos, iteration);
// Create an auxiliary stack
int stack[end - start + 1];
// push initial values of start and end to stack
stack[++top] = start;
stack[++top] = end;
qSort(windowWidth, windowHeight, maxNumber, foo, arrSize, stack, window, start, end, top);
window.display();
bool quickSort = false;
bool insertSort = false;
bool bubbleSort = false;
// run the program as long as the window is open
while (window.isOpen()) {
window.clear(sf::Color::Black);
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event{};
while (window.pollEvent(event)) {
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
quickSort = true;
insertSort = false;
bubbleSort = false;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::I)) {
quickSort = false;
insertSort = true;
bubbleSort = false;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::B)) {
quickSort = false;
insertSort = false;
bubbleSort = true;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
quickSort = false;
insertSort = false;
bubbleSort = false;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
for (int i = 0; i < arrSize; ++i) {
foo[i] = rand() % maxNumber;
}
shuffleArray(maxNumber, foo, arrSize, start, end, top, pos, iteration);
// push initial values of start and end to stack
stack[++top] = start;
stack[++top] = end;
window.clear(sf::Color::Black);
qSort(windowWidth, windowHeight, maxNumber, foo, arrSize, stack, window, start, end, top);
window.display();
quickSort = false;
insertSort = false;
bubbleSort = false;
}
}
}
if (quickSort) {
if (top != -1) {
qSort(windowWidth, windowHeight, maxNumber, foo, arrSize, stack, window, start, end,
top);
window.display();
} else {
quickSort = false;
}
} else if (insertSort) {
if (pos > -1) {
iSort(windowWidth, windowHeight, maxNumber, foo, arrSize, window, pos);
window.display();
} else {
insertSort = false;
}
} else if (bubbleSort) {
if (iteration < arrSize) {
bSort(windowWidth, windowHeight, maxNumber, foo, arrSize, window, iteration);
window.display();
} else {
bubbleSort = false;
}
}
}
return 0;
}