forked from CipherYuvraj/Algorithm-Visualiser-Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.cpp
More file actions
282 lines (227 loc) · 10.3 KB
/
sorting.cpp
File metadata and controls
282 lines (227 loc) · 10.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
#include "algorithms/sorting.h"
#include <algorithm>
#include <functional>
// SortingStep constructor implementation
SortingStep::SortingStep(const std::vector<int>& array,
const std::vector<int>& highlighted,
const std::vector<int>& comparing,
const std::string& operation,
int operations_count,
const std::string& time_complexity,
const std::string& space_complexity)
: array(array), highlighted(highlighted), comparing(comparing),
operation(operation), operations_count(operations_count),
time_complexity(time_complexity), space_complexity(space_complexity) {}
std::vector<SortingStep> bubbleSort(std::vector<int> arr) {
std::vector<SortingStep> steps;
int n = static_cast<int>(arr.size());
int operations = 0;
steps.push_back(SortingStep(arr, {}, {}, "Starting Bubble Sort", operations, "O(n²)", "O(1)"));
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
operations++;
std::vector<int> comparing = {j, j+1};
steps.push_back(SortingStep(arr, {}, comparing,
"Comparing " + std::to_string(arr[j]) + " and " + std::to_string(arr[j+1]),
operations, "O(n²)", "O(1)"));
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
std::vector<int> highlighted = {j, j+1};
steps.push_back(SortingStep(arr, highlighted, {},
"Swapped " + std::to_string(arr[j+1]) + " and " + std::to_string(arr[j]),
operations, "O(n²)", "O(1)"));
}
}
}
steps.push_back(SortingStep(arr, {}, {}, "Bubble Sort Complete", operations, "O(n²)", "O(1)"));
return steps;
}
std::vector<SortingStep> mergeSort(std::vector<int> arr) {
std::vector<SortingStep> steps;
int operations = 0;
steps.push_back(SortingStep(arr, {}, {}, "Starting Merge Sort", operations, "O(n log n)", "O(n)"));
std::function<void(int, int)> mergeSortHelper = [&](int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
std::vector<int> highlighted = {left, mid, right};
steps.push_back(SortingStep(arr, highlighted, {},
"Dividing array from " + std::to_string(left) + " to " + std::to_string(right),
operations, "O(n log n)", "O(n)"));
mergeSortHelper(left, mid);
mergeSortHelper(mid + 1, right);
// Merge process
std::vector<int> temp(right - left + 1);
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
operations++;
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) temp[k++] = arr[i++];
while (j <= right) temp[k++] = arr[j++];
for (int idx = 0; idx < k; idx++) {
arr[left + idx] = temp[idx];
}
std::vector<int> merged;
for (int idx = left; idx <= right; idx++) {
merged.push_back(idx);
}
steps.push_back(SortingStep(arr, merged, {},
"Merged subarrays", operations, "O(n log n)", "O(n)"));
}
};
mergeSortHelper(0, static_cast<int>(arr.size()) - 1);
steps.push_back(SortingStep(arr, {}, {}, "Merge Sort Complete", operations, "O(n log n)", "O(n)"));
return steps;
}
std::vector<SortingStep> quickSort(std::vector<int> arr) {
std::vector<SortingStep> steps;
int operations = 0;
steps.push_back(SortingStep(arr, {}, {}, "Starting Quick Sort", operations, "O(n log n)", "O(log n)"));
std::function<void(int, int)> quickSortHelper = [&](int low, int high) {
if (low < high) {
int pi = partition(arr, low, high, steps, operations);
quickSortHelper(low, pi - 1);
quickSortHelper(pi + 1, high);
}
};
quickSortHelper(0, static_cast<int>(arr.size()) - 1);
steps.push_back(SortingStep(arr, {}, {}, "Quick Sort Complete", operations, "O(n log n)", "O(log n)"));
return steps;
}
int partition(std::vector<int>& arr, int low, int high, std::vector<SortingStep>& steps, int& operations) {
int pivot = arr[high];
int i = low - 1;
std::vector<int> pivotHighlight = {high};
steps.push_back(SortingStep(arr, pivotHighlight, {}, "Choosing pivot: " + std::to_string(pivot),
operations, "O(n log n)", "O(log n)"));
for (int j = low; j < high; j++) {
operations++;
std::vector<int> comparing = {j};
steps.push_back(SortingStep(arr, {}, comparing,
"Comparing " + std::to_string(arr[j]) + " with pivot " + std::to_string(pivot),
operations, "O(n log n)", "O(log n)"));
if (arr[j] < pivot) {
i++;
std::swap(arr[i], arr[j]);
std::vector<int> swapped = {i, j};
steps.push_back(SortingStep(arr, swapped, {},
"Swapped " + std::to_string(arr[i]) + " and " + std::to_string(arr[j]),
operations, "O(n log n)", "O(log n)"));
}
}
std::swap(arr[i + 1], arr[high]);
std::vector<int> finalPos = {i + 1};
steps.push_back(SortingStep(arr, finalPos, {}, "Placed pivot in correct position",
operations, "O(n log n)", "O(log n)"));
return i + 1;
}
std::vector<SortingStep> heapSort(std::vector<int> arr) {
std::vector<SortingStep> steps;
int n = static_cast<int>(arr.size());
int operations = 0;
steps.push_back(SortingStep(arr, {}, {}, "Starting Heap Sort", operations, "O(n log n)", "O(1)"));
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i, steps, operations);
}
steps.push_back(SortingStep(arr, {}, {}, "Max heap built", operations, "O(n log n)", "O(1)"));
// Extract elements from heap
for (int i = n - 1; i > 0; i--) {
std::swap(arr[0], arr[i]);
operations++;
std::vector<int> swapped = {0, i};
steps.push_back(SortingStep(arr, swapped, {}, "Moved max element to position " + std::to_string(i),
operations, "O(n log n)", "O(1)"));
heapify(arr, i, 0, steps, operations);
}
steps.push_back(SortingStep(arr, {}, {}, "Heap Sort Complete", operations, "O(n log n)", "O(1)"));
return steps;
}
void heapify(std::vector<int>& arr, int n, int i, std::vector<SortingStep>& steps, int& operations) {
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]);
operations++;
std::vector<int> heapified = {i, largest};
steps.push_back(SortingStep(arr, heapified, {}, "Heapifying: swapped elements",
operations, "O(n log n)", "O(1)"));
heapify(arr, n, largest, steps, operations);
}
}
std::vector<SortingStep> shellSort(std::vector<int> arr) {
std::vector<SortingStep> steps;
int n = static_cast<int>(arr.size());
int operations = 0;
steps.push_back(SortingStep(arr, {}, {}, "Starting Shell Sort", operations, "O(n log² n)", "O(1)"));
for (int gap = n / 2; gap > 0; gap /= 2) {
steps.push_back(SortingStep(arr, {}, {},
"Current gap: " + std::to_string(gap),
operations, "O(n log² n)", "O(1)"));
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
steps.push_back(SortingStep(arr, {i}, {},
"Inserting element " + std::to_string(temp),
operations, "O(n log² n)", "O(1)"));
while (j >= gap && arr[j - gap] > temp) {
operations++;
arr[j] = arr[j - gap];
std::vector<int> moved = {j, j - gap};
steps.push_back(SortingStep(arr, moved, {},
"Moved " + std::to_string(arr[j]) + " right by gap",
operations, "O(n log² n)", "O(1)"));
j -= gap;
}
arr[j] = temp;
steps.push_back(SortingStep(arr, {j}, {},
"Placed " + std::to_string(temp) + " at correct position",
operations, "O(n log² n)", "O(1)"));
}
}
steps.push_back(SortingStep(arr, {}, {}, "Shell Sort Complete", operations, "O(n log² n)", "O(1)"));
return steps;
}
std::vector<SortingStep> countingSort(std::vector<int> arr) {
std::vector<SortingStep> steps;
int operations = 0;
if (arr.empty()) {
steps.push_back(SortingStep(arr, {}, {}, "Array is empty", operations, "O(n + k)", "O(k)"));
return steps;
}
int maxVal = *std::max_element(arr.begin(), arr.end());
int minVal = *std::min_element(arr.begin(), arr.end());
int range = maxVal - minVal + 1;
steps.push_back(SortingStep(arr, {}, {}, "Starting Counting Sort, range: " + std::to_string(range),
operations, "O(n + k)", "O(k)"));
std::vector<int> count(range, 0);
// Count frequencies
for (size_t i = 0; i < arr.size(); i++) {
count[arr[i] - minVal]++;
operations++;
}
steps.push_back(SortingStep(arr, {}, {}, "Counted element frequencies", operations, "O(n + k)", "O(k)"));
// Reconstruct array
int index = 0;
for (int i = 0; i < range; i++) {
while (count[i] > 0) {
arr[index] = i + minVal;
index++;
count[i]--;
operations++;
}
}
steps.push_back(SortingStep(arr, {}, {}, "Counting Sort Complete", operations, "O(n + k)", "O(k)"));
return steps;
}