-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-sorting.cpp
More file actions
395 lines (289 loc) · 11.3 KB
/
cpp-sorting.cpp
File metadata and controls
395 lines (289 loc) · 11.3 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
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
392
393
394
395
#include <iostream>
#include <random>
#include <algorithm>
#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>
#include "SFML_sorting.h"
bool is_sorting_done = false;
sf::RenderWindow window(sf::VideoMode(1500, 500), "Sorting");
sf::Vector2u windowSize = window.getSize();
int window_bottomX = 0;
int window_bottomY = windowSize.y;
int window_width = windowSize.x;
const int VECTOR_SIZE = 1600;
const uint16_t RECT_OFFSET = 0;
const float REFRESH_DELAY = 0;
const sf::Color DEFAULT_COLOR = sf::Color::Blue;
const sf::Color SELECTION_COLOR = sf::Color::Magenta;
const sf::Color SECONDARY_SELECTION_COLOR = sf::Color::Green;
float rect_width = (window_width - (VECTOR_SIZE * RECT_OFFSET)) / static_cast<float>(VECTOR_SIZE);
int screen_to_vector_ratio = std::ceil(VECTOR_SIZE / static_cast<double>(window_width));
int main() {
rect_width = (rect_width == 0) ? 1 : rect_width;
std::vector<int> unsort_vect = generate_random_vector(VECTOR_SIZE);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
if (!is_sorting_done) {
insertion_sort_visual(unsort_vect);
is_sorting_done = true;
}
//window.display();
}
return EXIT_SUCCESS;
}
// Рендеринг на основе VertexArray
void fill_vertex_array(sf::VertexArray& vert_array, const std::vector<int>& vect) {
vert_array.clear();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, 255);
sf::Color randomColor(dis(gen), dis(gen), dis(gen));
float height_offset = window_bottomY / static_cast<float>(VECTOR_SIZE);
int offset = 0;
bool current_rect;
for (int i = 0;i < vect.size(); i++) { // Нужнл что бы отрисовывались все 4 вертекса для прямоугольника, с учетом скипов
if (i % screen_to_vector_ratio != 0) {
offset += rect_width + RECT_OFFSET;
current_rect = true;
continue;
}
/*std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, 255);
sf::Color randomColor(dis(gen), dis(gen), dis(gen));*/
int vertex_origin_posX = window_bottomX + offset;
int vertex_origin_posY = window_bottomY;
int vert_top_offset = vect[i] * height_offset;
// l - left, r - right, b - bottom, t - top
sf::Vertex vertex_lb;
vertex_lb.position = sf::Vector2f(vertex_origin_posX, vertex_origin_posY);
vertex_lb.color = randomColor;
vert_array.append(vertex_lb);
sf::Vertex vertex_rb;
vertex_rb.position = sf::Vector2f(vertex_origin_posX + rect_width, vertex_origin_posY);
vertex_rb.color = randomColor;
vert_array.append(vertex_rb);
randomColor = sf::Color(dis(gen), dis(gen), dis(gen));
sf::Vertex vertex_rt;
vertex_rt.position = sf::Vector2f(vertex_origin_posX + rect_width, vertex_origin_posY - vert_top_offset);
vertex_rt.color = randomColor;
vert_array.append(vertex_rt);
sf::Vertex vertex_lt;
vertex_lt.position = sf::Vector2f(vertex_origin_posX, vertex_origin_posY - vert_top_offset);
vertex_lt.color = randomColor;
vert_array.append(vertex_lt);
offset += rect_width + RECT_OFFSET;
}
}
void bogo_sort_visual(const std::vector<int>& unsorted_vector) {
std::vector<sf::RectangleShape> rect_vector;
}
// Перераспледелить window.clear() и window.display() для оптимизации
bool is_sorted_visual(const std::vector<int>& vect) {
sf::VertexArray vert_array(sf::Quads);
fill_vertex_array(vert_array, vect);
for (size_t i = 0; i < vect.size(); i++) {
//sf::sleep(sf::seconds(REFRESH_DELAY/2));
change_vert_color(vert_array, i, SECONDARY_SELECTION_COLOR);
//draw_rectangles(rect_vector);
draw_vertexes(vert_array);
int left_num = vect.at(i);
int right_num;
if (i == vect.size() - 1) {
right_num = left_num;
}
else {
right_num = vect.at(i + 1);
}
if (left_num > right_num) return false;
}
return true;
}
void insertion_sort_visual(const std::vector<int>& unsorted_vector) {
std::vector<sf::RectangleShape> rect_vector;
sf::VertexArray vert_array(sf::Quads);
std::vector<int> buffer;
std::vector<int> combined_vector;
for (int i = 0; i < unsorted_vector.size(); i++) {
// Наложение векторов для визуальной видимости процесса сортировки
combined_vector = combine_vectors(buffer, buffer.end(), unsorted_vector);
fill_vertex_array(vert_array, combined_vector);
//change_vert_color(vert_array, i, SECONDARY_SELECTION_COLOR);
draw_vertexes(vert_array);
int buffer_num = unsorted_vector[i];
for (int j = 0; j < unsorted_vector.size(); j++) {
sf::sleep(sf::seconds(REFRESH_DELAY));
if (j % 1 == 0 && j != 0) {
//fill_vertex_array(vert_array, combined_vector);
//reset_vert_colors(vert_array);
//change_vert_color(vert_array, j-1, DEFAULT_COLOR);
//change_vert_color(vert_array, j, SELECTION_COLOR);
//draw_vertexes(vert_array);
}
if (j >= buffer.size()) {
buffer.push_back(buffer_num);
break;
}
if (buffer[j] >= buffer_num) {
buffer.insert(buffer.begin() + j, buffer_num);
break;
}
else {
continue;
}
}
}
combined_vector = combine_vectors(buffer, buffer.end(), unsorted_vector);
fill_vertex_array(vert_array, combined_vector);
draw_vertexes(vert_array);
//is_sorted_visual(combined_vector);
}
void draw_vertexes(const sf::VertexArray& vert_array) {
window.clear();
window.draw(vert_array);
window.display();
}
std::vector<int> combine_vectors(const std::vector<int>& first, std::vector<int>::const_iterator merge_pos, const std::vector<int>& second) {
std::vector<int> combined_vector;
combined_vector.insert(combined_vector.begin(), first.begin(), merge_pos);
if (first.size() + second.begin() == second.end()) return combined_vector;
combined_vector.insert(combined_vector.end(), second.begin() + first.size(), second.end());
return combined_vector;
}
void bubble_sort_visual(const std::vector<int>& unsorted_vector) {
std::vector<int> sort_vector = unsorted_vector;
//std::vector<sf::RectangleShape> rect_vector;
sf::VertexArray vert_array(sf::Quads);
int sort_vector_index = 0;
int last_selected_index = 0;
while (/*!std::is_sorted(sort_vector.begin(), sort_vector.end())*/!is_sorted_visual(sort_vector)) {
sf::sleep(sf::seconds(REFRESH_DELAY));
fill_vertex_array(vert_array, sort_vector);
if (sort_vector_index < sort_vector.size() - 1) {
int& left_num = sort_vector[sort_vector_index];
change_vert_color(vert_array, sort_vector_index, SELECTION_COLOR);
int& right_num = sort_vector[sort_vector_index + 1];
change_vert_color(vert_array, sort_vector_index + 1, SECONDARY_SELECTION_COLOR);
draw_vertexes(vert_array);
if (left_num > right_num)
std::swap(left_num, right_num);
}
if (sort_vector_index > sort_vector.size() - 1) sort_vector_index = 0;
else sort_vector_index++;
}
fill_vertex_array(vert_array, sort_vector); // Последний апдейт после полной сортировки
draw_vertexes(vert_array);
}
void rect_vector_from_int(std::vector<sf::RectangleShape>& rect_vector, const std::vector<int>& vect) {
rect_vector.clear();
int offset = 0;
for (int i = 0; i < vect.size(); i++) // Отрисовка вектора в виде прямоугольников
{
int value = vect[i];
sf::RectangleShape rect(sf::Vector2f(rect_width, value * -4));
rect.setFillColor(sf::Color::Blue);
rect.setOrigin(-window_bottomX, -window_bottomY);
rect.setPosition(offset, 0);
offset += rect_width + RECT_OFFSET;
rect_vector.push_back(rect);
}
//draw_rectangles(rect_vector);
}
void reset_vert_colors(sf::VertexArray& vert_array) {
for (int i = 0; i < vert_array.getVertexCount(); i++)
{
vert_array[i].color = DEFAULT_COLOR;
}
}
//void change_rect_color(std::vector<sf::RectangleShape>& rect_vector, int index, sf::Color color) {
// rect_vector[index].setFillColor(color);
// draw_rectangles(rect_vector);
//}
void change_vert_color(sf::VertexArray& vert_array, int index, sf::Color color) {
index *= 4;
for (int i = 0; i <= 4; i++)
{
vert_array[index + i].color = color;
}
}
void draw_rectangles(const std::vector<sf::RectangleShape>& rect_vector) {
window.clear();
for (const sf::RectangleShape rect : rect_vector) {
window.draw(rect);
}
window.display();
}
// O(n^2)
std::vector<int> bubble_sort(const std::vector<int>& intVect) {
std::vector<int> sortVect = intVect;
int i = 0;
int iteration = 0;
while (!std::is_sorted(sortVect.begin(), sortVect.end())) {
if (i < sortVect.size() - 1) {
if (sortVect[i] > sortVect[i + 1]) std::swap(sortVect[i], sortVect[i + 1]);
}
i++;
if (i > sortVect.size() - 1) i = 0;
iteration++;
}
std::cout << "Iter count: " + std::to_string(iteration) << std::endl;
return sortVect;
}
// O(n)
std::vector<int> insertion_sort(const std::vector<int>& intVect) {
std::vector<int> sortVect = intVect;
std::vector<int> buffer;
int iteration = 0;
for (int i = 0; i < sortVect.size(); i++) {
int buffer_num = sortVect[i];
for (int j = 0; j < sortVect.size(); j++) {
if (j >= buffer.size()) {
buffer.push_back(buffer_num);
break;
}
if (buffer[j] >= buffer_num) {
buffer.insert(buffer.begin() + j, buffer_num);
break;
}
else {
continue;
}
}
iteration++;
}
std::cout << "Iter count: " + std::to_string(iteration) << std::endl;
return buffer;
}
std::vector<int> generate_random_vector(int vector_size) {
/*std::vector<int> rand_array;
std::random_device random;
std::mt19937 mt(random());
std::distrib<int> distribution(;*/
std::vector<int> source_vector;
for (size_t i = 1; i < vector_size; i++)
{
source_vector.push_back(i);
}
std::random_shuffle(source_vector.begin(), source_vector.end());
/*for (size_t i = 0; i < source_vector.size(); i++)
{
}*/
/*for (int i = 0; i < vector_size; ++i) {
rand_array.push_back(distribution(mt));
}*/
return source_vector;
}
void print_vector(const std::vector<int>& intVect) {
std::cout << "[";
for (int i = 0; i < intVect.size(); i++) {
std::cout << intVect[i];
if (i == intVect.size() - 1) continue;
std::cout << ", ";
}
std::cout << "]";
}