-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
327 lines (269 loc) · 8.54 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
324
325
326
327
#include <iostream>
#include <SFML/Graphics.hpp>
#include "imgui-1.89.4/imgui.h"
#include "imgui-1.89.4/imgui-SFML.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include "src/gtime.h"
#include <vector>
#include <string.h>
#include <chrono>
#include <sstream>
#include <thread>
using namespace std;
bool showWindow = true;
int generateRandom(int fMin, int fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
double generateRandomDouble(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
class SortingCore
{
public:
// sorting methods
enum SortType {
QUICK_SORT,
BUBBLE_SORT,
HEAP_SORT
};
SortType sortMethod = SortType::QUICK_SORT;
bool canStartSort = false;
bool renderStepByStep = true;
int sortStepDelay = 100;
int datasetCount = 250;
double maxVal = 10000.0;
vector<double> sortDataset = vector<double>();
sf::RenderWindow* renderWindow;
string sortingResults = "No results";
SortingCore(sf::RenderWindow& window)
{
renderWindow = &window;
CreateArray();
}
~SortingCore()
{
sortDataset.clear();
}
void CreateArray(){
sortDataset.clear();
for (size_t i = 0; i < datasetCount; i++)
{
double val = generateRandomDouble(0.0, maxVal);
sortDataset.push_back(val);
}
}
// Partition the array into two parts and return the index of the pivot element
int partition(std::vector<double>& arr, int low, int high) {
double pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; ++j) {
if (arr[j] < pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
RenderStepWithDelay();
return i + 1;
}
// Recursive function to perform Quick Sort
void quickSort(std::vector<double>& arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void bubbleSort(std::vector<double>& arr) {
int n = arr.size();
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
swapped = true;
}
}
RenderStepWithDelay();
// If no two elements were swapped in the inner loop, the array is already sorted
if (!swapped) {
break;
}
}
}
void heapify(std::vector<double>& arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
std::swap(arr[i], arr[largest]);
RenderStepWithDelay();
heapify(arr, n, largest);
}
}
void heapSort(std::vector<double>& arr) {
int n = arr.size();
// Build a max-heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; --i) {
heapify(arr, n, i);
}
// Extract elements from the heap one by one
for (int i = n - 1; i > 0; --i) {
std::swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void RenderStepWithDelay(){
//if(!renderStepByStep) return;
//renderWindow->clear();
//Update(*renderWindow);
//renderWindow->display();
sf::sleep(sf::milliseconds(sortStepDelay));
}
bool isSorting()
{
return canStartSort;
}
void Sort()
{
while (true)
{
if(!canStartSort) continue;
showWindow = false;
auto startTime = std::chrono::high_resolution_clock::now();
switch (sortMethod)
{
case QUICK_SORT:
quickSort(sortDataset, 0, sortDataset.size() - 1);
break;
case BUBBLE_SORT:
bubbleSort(sortDataset);
break;
case HEAP_SORT:
heapSort(sortDataset);
break;
default:
quickSort(sortDataset, 0, sortDataset.size() - 1);
break;
}
auto endTime = std::chrono::high_resolution_clock::now();
// Calculate the duration
auto duration_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count();
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
auto duration_s = std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count();
std::stringstream stream;
stream << "Execution time: " << duration_s << " s, " << duration_ms << " ms, (" << duration_ns << ") ns" << std::endl;
sortingResults = stream.str();
canStartSort = false;
showWindow = true;
}
}
void Update(sf::RenderWindow& window)
{
int count = sortDataset.size();
sf::Vector2u wSize = window.getSize();
double col_width = wSize.x / count;
for (size_t i = 0; i < count; i++)
{
double col_height = wSize.y * sortDataset[i] / maxVal;
sf::RectangleShape rect(sf::Vector2f(col_width, col_height));
rect.setPosition(sf::Vector2f(col_width * i, 0));
window.draw(rect);
}
}
};
void DrawGui(SortingCore& sortingCore)
{
ImGui::SetNextWindowCollapsed(!showWindow);
ImGui::Begin("Settings", &showWindow);
ImGui::Text("Is sorting dataset: %s", sortingCore.isSorting()? "true": "false");
ImGui::Text("%s", sortingCore.sortingResults.c_str());
ImGui::Text("%s", "Show sorting steps:");
ImGui::Checkbox("Show sorting steps", &sortingCore.renderStepByStep);
ImGui::Text("%s", "Dataset count");
ImGui::InputInt("Dataset count", &sortingCore.datasetCount);
ImGui::Text("%s", "Sort step delay (ms)");
ImGui::InputInt("Sort step delay", &sortingCore.sortStepDelay);
if (ImGui::Button("Create new array")) {
sortingCore.CreateArray();
}
if (ImGui::Button("Quick sort")) {
sortingCore.sortMethod = sortingCore.QUICK_SORT;
sortingCore.canStartSort = true;
}
if (ImGui::Button("Bubble sort")) {
sortingCore.sortMethod = sortingCore.BUBBLE_SORT;
sortingCore.canStartSort = true;
}
if (ImGui::Button("Heap sort")) {
sortingCore.sortMethod = sortingCore.HEAP_SORT;
sortingCore.canStartSort = true;
}
ImGui::End();
}
void SortingCoreInitialize(){
sf::RenderWindow window(sf::VideoMode(800, 600), "Sorting algorithms");
window.setFramerateLimit(60);
SortingCore sortingCore(window);
std::thread sortingThread([&sortingCore]() {
sortingCore.Sort();
});
sortingThread.detach();
bool buttonClicked = false;
// Initialize ImGui
ImGui::SFML::Init(window);
// Main loop
while (window.isOpen())
{
ProgramTime::RestartClock();
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event);
if (event.type == sf::Event::Closed) {
window.close();
}
else
{
if (event.type == sf::Event::Resized) {
// Handle window resize
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::A) {
//sortingCore.Sort();
}
}
}
}
ImGui::SFML::Update(window, ProgramTime::elapsedTime);
DrawGui(sortingCore);
// Clear the windows
window.clear();
sortingCore.Update(window);
ImGui::SFML::Render(window);
window.display();
}
// Shutdown ImGui
ImGui::SFML::Shutdown();
}
int main()
{
srand(time(NULL));
SortingCoreInitialize();
return 0;
}