-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquicksort.cpp
More file actions
204 lines (163 loc) · 4.5 KB
/
quicksort.cpp
File metadata and controls
204 lines (163 loc) · 4.5 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
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <thread>
#include <algorithm>
#include <iomanip>
using namespace std;
const int ARRAY_SIZE = 20;
const int DELAY_MS = 100;
const int BAR_WIDTH = 3;
const char BAR_CHAR = '#';
const char CURRENT_MIN_CHAR = '*';
const char COMPARING_CHAR = '?';
const char SWAPPING_CHAR = 'X';
const char SORTED_CHAR = '@';
void clearScreen()
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void delay()
{
this_thread::sleep_for(chrono::milliseconds(DELAY_MS));
}
void visualizeArray(const vector<int> &arr,
const vector<char> &markers,
int currentPos = -1,
int minPos = -1,
int comparing = -1,
bool swapping = false)
{
clearScreen();
int maxVal = *max_element(arr.begin(), arr.end());
cout << "SelectionSort Visualization" << endl;
cout << "==========================" << endl
<< endl;
cout << BAR_CHAR << " - Unsorted element "
<< CURRENT_MIN_CHAR << " - Current minimum "
<< COMPARING_CHAR << " - Comparing "
<< SORTED_CHAR << " - Sorted element" << endl
<< endl;
int maxHeight = 15;
for (int h = maxHeight; h > 0; h--)
{
for (int i = 0; i < arr.size(); i++)
{
int barHeight = static_cast<int>((static_cast<double>(arr[i]) / maxVal) * maxHeight);
if (barHeight >= h)
{
char displayChar = markers[i];
for (int w = 0; w < BAR_WIDTH; w++)
{
cout << displayChar;
}
}
else
{
for (int w = 0; w < BAR_WIDTH; w++)
{
cout << ' ';
}
}
}
cout << endl;
}
for (int i = 0; i < arr.size() * BAR_WIDTH; i++)
{
cout << "-";
}
cout << endl;
for (int i = 0; i < arr.size(); i++)
{
cout << setw(BAR_WIDTH) << i;
}
cout << endl;
for (int i = 0; i < arr.size(); i++)
{
cout << setw(BAR_WIDTH) << arr[i];
}
cout << endl
<< endl;
if (currentPos >= 0)
{
cout << "Current position: " << currentPos << endl;
}
if (minPos >= 0)
{
cout << "Current minimum: arr[" << minPos << "] = " << arr[minPos] << endl;
}
if (comparing >= 0)
{
cout << "Comparing with: arr[" << comparing << "] = " << arr[comparing] << endl;
}
if (swapping && currentPos >= 0 && minPos >= 0)
{
cout << "Swapping arr[" << currentPos << "] = " << arr[currentPos]
<< " and arr[" << minPos << "] = " << arr[minPos] << endl;
}
delay();
}
void selectionSort(vector<int> &arr)
{
vector<char> markers(arr.size(), BAR_CHAR);
int n = arr.size();
for (int i = 0; i < n - 1; i++)
{
int minIdx = i;
markers[i] = COMPARING_CHAR;
visualizeArray(arr, markers, i, minIdx);
markers[minIdx] = CURRENT_MIN_CHAR;
for (int j = i + 1; j < n; j++)
{
markers[j] = COMPARING_CHAR;
visualizeArray(arr, markers, i, minIdx, j);
if (arr[j] < arr[minIdx])
{
markers[minIdx] = BAR_CHAR;
minIdx = j;
markers[minIdx] = CURRENT_MIN_CHAR;
visualizeArray(arr, markers, i, minIdx);
}
markers[j] = BAR_CHAR;
}
if (minIdx != i)
{
markers[i] = SWAPPING_CHAR;
markers[minIdx] = SWAPPING_CHAR;
visualizeArray(arr, markers, i, minIdx, -1, true);
swap(arr[i], arr[minIdx]);
visualizeArray(arr, markers, i, minIdx);
markers[minIdx] = BAR_CHAR;
}
markers[i] = SORTED_CHAR;
visualizeArray(arr, markers, i);
}
markers[n - 1] = SORTED_CHAR;
visualizeArray(arr, markers);
}
int main()
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dist(1, 99);
vector<int> array(ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
{
array[i] = dist(gen);
}
vector<char> markers(ARRAY_SIZE, BAR_CHAR);
cout << "Initial array:" << endl;
visualizeArray(array, markers);
cout << "Press Enter to start SelectionSort...";
cin.get();
selectionSort(array);
cout << "Sorting completed!" << endl;
cout << "Press Enter to exit...";
cin.get();
return 0;
}