-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
353 lines (277 loc) · 8.9 KB
/
Copy pathscript.js
File metadata and controls
353 lines (277 loc) · 8.9 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
let isPaused = false; // Global flag to handle pause
let isSorting = false; // Prevent multiple sorting operations at the same time
let animationSpeed = 700; // Default animation speed
// Dynamically create bars for visualization
const visualizer = document.getElementById('visualizer');
let array = [];
// Utility function to delay animation
function delay(speed = animationSpeed) {
return new Promise((resolve) => {
let interval = setInterval(() => {
if (!isPaused) {
clearInterval(interval);
resolve();
}
}, speed);
});
}
function toggleBarsPosition() {
const visualizer = document.getElementById('visualizer');
visualizer.classList.toggle('upside-down');
}
function changeBarColors() {
const bars = document.querySelectorAll('.bar');
bars.forEach(bar => {
const randomColor = getRandomColor();
bar.style.backgroundColor = randomColor;
});
}
function handleButtonPress(button, action) {
// Clear active state from all buttons
document.querySelectorAll('button').forEach((btn) => btn.classList.remove('active'));
// Set the clicked button as active
button.classList.add('active');
// Execute the corresponding action
if (action === 'randomizeArray') randomizeArray();
else if (action === 'changeSize') changeSize();
else if (action === 'pause') pause();
else if (action === 'resetBarColors') resetBarColors();
else if (['bubble', 'quick', 'merge', 'insertion', 'selection'].includes(action)) startSort(action);
else alert('Functionality not yet implemented!');
}
// Generate a randomized array
function randomizeArray() {
if (isSorting) return alert("Wait for the current sorting to finish!");
visualizer.innerHTML = ''; // Clear existing bars
array = []; // Reset array
const arraySize = 50; // Number of bars
for (let i = 0; i < arraySize; i++) {
const value = Math.floor(Math.random() * 200) + 50; // Bar height
array.push(value);
const bar = document.createElement('div');
bar.className = 'bar';
bar.style.height = `${value}px`;
visualizer.appendChild(bar);
}
}
async function swapBars(bar1, bar2) {
// Temporarily disable transitions to force reflow (ensures smooth animation)
bar1.style.transition = 'none';
bar2.style.transition = 'none';
// Trigger reflow by accessing a layout property
void bar1.offsetHeight;
void bar2.offsetHeight;
// Re-enable transitions
bar1.style.transition = 'height 0.5s ease';
bar2.style.transition = 'height 0.5s ease';
// Swap heights with a transition
const tempHeight = bar1.style.height;
bar1.style.height = bar2.style.height;
bar2.style.height = tempHeight;
// Wait for the transition to complete
await delay(animationSpeed);
}
// Reset bar colors
function resetBarColors() {
const bars = document.querySelectorAll('.bar');
bars.forEach((bar) => {
bar.style.backgroundColor = 'blue';
});
}
// Pause and Resume
function pause() {
isPaused = !isPaused;
document.querySelector('[onclick="pause()"]').innerText = isPaused ? 'Resume' : 'Pause';
}
function changeSize() {
const newSize = prompt('Enter the number of bars (5-100):', 20);
if (newSize && newSize >= 5 && newSize <= 100) {
visualizer.innerHTML = ''; // Clear current bars
array = [];
const arraySize = Number(newSize);
for (let i = 0; i < arraySize; i++) {
const value = Math.floor(Math.random() * 200) + 50; // Random bar heights
array.push(value);
const bar = document.createElement('div');
bar.className = 'bar';
bar.style.height = `${value}px`;
visualizer.appendChild(bar);
}
} else {
alert('Invalid size. Please enter a number between 5 and 100.');
}
}
async function insertionSort() {
const bars = document.querySelectorAll('.bar');
for (let i = 1; i < array.length; i++) {
let key = array[i];
let j = i - 1;
// Highlight the current bar being sorted
bars[i].style.backgroundColor = 'red';
// Shift elements and animate the swap
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
// Swap the bars visually
await swapBars(bars[j + 1], bars[j]);
j--;
}
array[j + 1] = key;
// Reset colors
bars[i].style.backgroundColor = 'blue';
}
}
function updateSpeed(value) {
animationSpeed = Number(value);
document.getElementById('speed-value').innerText = `${value}ms`;
// Update bar transition durations dynamically
const bars = document.querySelectorAll('.bar');
bars.forEach(bar => {
bar.style.transition = `height ${animationSpeed / 1000}s ease`;
});
}
async function selectionSort() {
const bars = document.querySelectorAll('.bar');
for (let i = 0; i < array.length - 1; i++) {
let minIndex = i;
bars[minIndex].style.backgroundColor = 'red';
for (let j = i + 1; j < array.length; j++) {
bars[j].style.backgroundColor = 'yellow';
await delay();
if (array[j] < array[minIndex]) {
if (minIndex !== i) bars[minIndex].style.backgroundColor = 'blue';
minIndex = j;
} else {
bars[j].style.backgroundColor = 'blue';
}
}
if (minIndex !== i) {
[array[i], array[minIndex]] = [array[minIndex], array[i]];
// Animate the swapping of bars
await swapBars(bars[i], bars[minIndex]);
}
bars[minIndex].style.backgroundColor = 'blue';
bars[i].style.backgroundColor = 'blue';
}
}
// Bubble Sort
async function bubbleSort() {
const bars = document.querySelectorAll('.bar');
isSorting = true;
for (let i = 0; i < array.length - 1; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
bars[j].style.backgroundColor = 'red';
bars[j + 1].style.backgroundColor = 'red';
if (array[j] > array[j + 1]) {
// Swap values in the array
[array[j], array[j + 1]] = [array[j + 1], array[j]];
// Animate the swapping of bars
await swapBars(bars[j], bars[j + 1]);
}
bars[j].style.backgroundColor = 'blue';
bars[j + 1].style.backgroundColor = 'blue';
}
}
isSorting = false;
}
// Quick Sort
async function partition(low, high, bars) {
let pivot = array[high];
let i = low - 1;
bars[high].style.backgroundColor = 'yellow'; // Pivot
for (let j = low; j < high; j++) {
bars[j].style.backgroundColor = 'red';
if (array[j] < pivot) {
i++;
// Swap array elements and animate bars
[array[i], array[j]] = [array[j], array[i]];
await swapBars(bars[i], bars[j]);
}
bars[j].style.backgroundColor = 'blue';
}
// Swap pivot into the correct position
[array[i + 1], array[high]] = [array[high], array[i + 1]];
await swapBars(bars[i + 1], bars[high]);
bars[high].style.backgroundColor = 'blue';
return i + 1;
}
async function quickSortHelper(low, high, bars) {
if (low < high) {
let pivotIndex = await partition(low, high, bars);
await quickSortHelper(low, pivotIndex - 1, bars);
await quickSortHelper(pivotIndex + 1, high, bars);
}
}
async function quickSort() {
const bars = document.querySelectorAll('.bar');
isSorting = true;
await quickSortHelper(0, array.length - 1, bars);
isSorting = false;
}
async function quickSort() {
const bars = document.querySelectorAll('.bar');
isSorting = true;
await quickSortHelper(0, array.length - 1, bars);
isSorting = false;
}
// Merge Sort
async function merge(start, mid, end, bars) {
let left = start;
let right = mid + 1;
let temp = [];
while (left <= mid && right <= end) {
if (array[left] <= array[right]) {
temp.push(array[left++]);
} else {
temp.push(array[right++]);
}
}
while (left <= mid) temp.push(array[left++]);
while (right <= end) temp.push(array[right++]);
// Apply the sorted section back to the bars with animation
for (let i = start; i <= end; i++) {
array[i] = temp[i - start];
bars[i].style.height = `${array[i]}px`;
bars[i].style.backgroundColor = 'red';
await delay();
bars[i].style.backgroundColor = 'blue';
}
}
async function mergeSortHelper(start, end, bars) {
if (start >= end) return;
let mid = Math.floor((start + end) / 2);
await mergeSortHelper(start, mid, bars);
await mergeSortHelper(mid + 1, end, bars);
await merge(start, mid, end, bars);
}
async function mergeSort() {
const bars = document.querySelectorAll('.bar');
isSorting = true;
await mergeSortHelper(0, array.length - 1, bars);
isSorting = false;
}
// Add functionality to each button
function startSort(algorithm) {
if (isSorting) return alert("Wait for the current sorting to finish!");
resetBarColors();
switch (algorithm) {
case 'bubble':
bubbleSort();
break;
case 'quick':
quickSort();
break;
case 'merge':
mergeSort();
break;
case 'insertion':
insertionSort();
break;
case 'selection':
selectionSort();
break;
default:
alert('Sorting algorithm not implemented yet!');
}
}
// Initialize the visualizer with randomized bars
randomizeArray();