-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
496 lines (379 loc) · 14.7 KB
/
scripts.js
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//complete
async function bubbleSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
for (let i = 0; i < len - 1; i++) {
for (let j = 0; j < len - i - 1; j++) {
let current = elements[j];
let next = elements[j + 1];
current.style.backgroundColor = '#CAD6E3';
next.style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
let currentHeight = current.style.height;
let nextHeight = next.style.height;
if (parseInt(currentHeight) > parseInt(nextHeight)) {
// Swap heights
current.style.height = nextHeight;
next.style.height = currentHeight;
// Change color after swapping
current.style.backgroundColor = '#85C1E9';
next.style.backgroundColor = '#85C1E9';
}
await new Promise(resolve => setTimeout(resolve, 500));
// Reset colors
current.style.backgroundColor = '';
next.style.backgroundColor = '';
}
}
}
//complete
function mergeSort() {
let container = document.getElementById('container');
let elements = container.children;
mergeSortRecursive(elements, 0, elements.length - 1);
}
//complete
// Recursive Merge Sort function
async function mergeSortRecursive(arr, left, right) {
if (left < right) {
const middle = Math.floor((left + right) / 2);
// Change color before dividing
for (let i = left; i <= right; i++) {
arr[i].style.backgroundColor = '#CAD6E3';
}
await new Promise(resolve => setTimeout(resolve, 500));
// Recursively sort the first and second halves
await mergeSortRecursive(arr, left, middle);
await mergeSortRecursive(arr, middle + 1, right);
// Merge the sorted halves
await merge(arr, left, middle, right);
}
}
//complete
// Merge function to merge two sorted arrays
async function merge(arr, left, middle, right) {
let leftArray = [];
let rightArray = [];
for (let i = left; i <= middle; i++) {
leftArray.push(arr[i].style.height);
}
for (let i = middle + 1; i <= right; i++) {
rightArray.push(arr[i].style.height);
}
let i = left, j = 0, k = 0;
while (j < leftArray.length && k < rightArray.length) {
if (parseInt(leftArray[j]) <= parseInt(rightArray[k])) {
arr[i].style.height = leftArray[j];
j++;
} else {
arr[i].style.height = rightArray[k];
k++;
}
i++;
// Change color during merging
arr[i - 1].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
arr[i - 1].style.backgroundColor = '';
}
while (j < leftArray.length) {
arr[i].style.height = leftArray[j];
j++;
i++;
// Change color during merging
arr[i - 1].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
arr[i - 1].style.backgroundColor = '';
}
while (k < rightArray.length) {
arr[i].style.height = rightArray[k];
k++;
i++;
// Change color during merging
arr[i - 1].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
arr[i - 1].style.backgroundColor = '';
}
}
//complete
function generateRandomArray() {
let container = document.getElementById('container');
container.innerHTML = '';
for (let i = 0; i < 10; i++) {
let height = Math.floor(Math.random() * 100) + 20;
let element = document.createElement('div');
element.className = 'element';
element.style.height = height + 'px';
container.appendChild(element);
}
}
//complete
async function gnomeSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
let i = 0;
while (i < len) {
if (i === 0 || parseInt(elements[i].style.height) >= parseInt(elements[i - 1].style.height)) {
elements[i].style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
elements[i].style.backgroundColor = '';
i++;
} else {
// Swap heights
let current = elements[i];
let prev = elements[i - 1];
current.style.backgroundColor = '#CAD6E3';
prev.style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
let currentHeight = current.style.height;
let prevHeight = prev.style.height;
// Swap heights
current.style.height = prevHeight;
prev.style.height = currentHeight;
await new Promise(resolve => setTimeout(resolve, 500));
// Reset colors after swapping
current.style.backgroundColor = '';
prev.style.backgroundColor = '';
// Move back in the array
i--;
}
}
}
//complete
async function bucketSort() {
let container = document.getElementById('container');
let elements = container.children;
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
// Find the minimum and maximum values
for (let i = 0; i < elements.length; i++) {
let height = parseInt(elements[i].style.height);
if (height < min) min = height;
if (height > max) max = height;
}
let bucketSize = 5; // Adjust the bucket size as needed
let numBuckets = Math.ceil((max - min + 1) / bucketSize);
let buckets = new Array(numBuckets);
// Initialize buckets
for (let i = 0; i < numBuckets; i++) {
buckets[i] = [];
}
// Distribute elements into buckets
for (let i = 0; i < elements.length; i++) {
let height = parseInt(elements[i].style.height);
let bucketIndex = Math.floor((height - min) / bucketSize);
buckets[bucketIndex].push(elements[i]);
}
// Visualize buckets (Pink and Blue)
for (let i = 0; i < buckets.length; i++) {
let color = i % 2 === 0 ? '#9BBBE9' : '#A8DCD9'; // Pink and Blue colors
for (let j = 0; j < buckets[i].length; j++) {
buckets[i][j].style.backgroundColor = color;
await new Promise(resolve => setTimeout(resolve, 500));
}
}
// Concatenate buckets back into the container
container.innerHTML = '';
for (let i = 0; i < buckets.length; i++) {
container.append(...buckets[i]);
}
}
//complete
async function stoogeSort() {
let container = document.getElementById('container');
let elements = container.children;
await stoogeSortRecursive(elements, 0, elements.length - 1);
}
//complete
async function stoogeSortRecursive(arr, l, h) {
if (l >= h) {
return;
}
if (parseInt(arr[l].style.height) > parseInt(arr[h].style.height)) {
// Swap heights
let temp = arr[l].style.height;
arr[l].style.height = arr[h].style.height;
arr[h].style.height = temp;
// Visualize swapping
arr[l].classList.add('comparison');
arr[h].classList.add('comparison');
await new Promise(resolve => setTimeout(resolve, 500));
arr[l].classList.remove('comparison');
arr[h].classList.remove('comparison');
}
if (h - l + 1 > 2) {
let t = Math.floor((h - l + 1) / 3);
await stoogeSortRecursive(arr, l, h - t);
await stoogeSortRecursive(arr, l + t, h);
await stoogeSortRecursive(arr, l, h - t);
}
}
//complete
async function combSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
let gap = len;
let shrinkFactor = 1.3;
let swapped = true;
while (gap > 1 || swapped) {
if (gap > 1) {
gap = Math.floor(gap / shrinkFactor);
}
swapped = false;
for (let i = 0; i + gap < len; i++) {
let current = elements[i];
let next = elements[i + gap];
current.style.backgroundColor = '#CAD6E3';
next.style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
let currentHeight = current.style.height;
let nextHeight = next.style.height;
if (parseInt(currentHeight) > parseInt(nextHeight)) {
// Swap heights
current.style.height = nextHeight;
next.style.height = currentHeight;
// Change color after swapping
current.style.backgroundColor = '#85C1E9';
next.style.backgroundColor = '#85C1E9';
swapped = true;
}
await new Promise(resolve => setTimeout(resolve, 500));
// Reset colors
current.style.backgroundColor = '';
next.style.backgroundColor = '';
}
}
}
//complete
async function selectionSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
for (let i = 0; i < len - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < len; j++) {
let current = elements[j];
let minElement = elements[minIndex];
current.style.backgroundColor = '#CAD6E3';
minElement.style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
let currentHeight = parseInt(current.style.height);
let minElementHeight = parseInt(minElement.style.height);
if (currentHeight < minElementHeight) {
minIndex = j;
}
current.style.backgroundColor = '';
minElement.style.backgroundColor = '';
}
// Swap heights
let tempHeight = elements[i].style.height;
elements[i].style.height = elements[minIndex].style.height;
elements[minIndex].style.height = tempHeight;
// Change color after swapping
elements[i].style.backgroundColor = '#85C1E9';
elements[minIndex].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
// Reset colors
elements[i].style.backgroundColor = '';
elements[minIndex].style.backgroundColor = '';
}
}
//complete
async function quickSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
async function partition(low, high) {
let pivot = parseInt(elements[high].style.height);
let i = low - 1;
for (let j = low; j < high; j++) {
let currentHeight = parseInt(elements[j].style.height);
if (currentHeight < pivot) {
i++;
let temp = elements[i].style.height;
elements[i].style.height = elements[j].style.height;
elements[j].style.height = temp;
elements[i].style.backgroundColor = '#CAD6E3';
elements[j].style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
elements[i].style.backgroundColor = '';
elements[j].style.backgroundColor = '';
}
}
let temp = elements[i + 1].style.height;
elements[i + 1].style.height = elements[high].style.height;
elements[high].style.height = temp;
elements[i + 1].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
elements[i + 1].style.backgroundColor = '';
return i + 1;
}
async function quickSortRecursive(low, high) {
if (low < high) {
let pivotIndex = await partition(low, high);
await quickSortRecursive(low, pivotIndex - 1);
await quickSortRecursive(pivotIndex + 1, high);
}
}
await quickSortRecursive(0, len - 1);
}
//complete
async function heapSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
async function heapify(n, i) {
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && parseInt(elements[left].style.height) > parseInt(elements[largest].style.height)) {
largest = left;
}
if (right < n && parseInt(elements[right].style.height) > parseInt(elements[largest].style.height)) {
largest = right;
}
if (largest !== i) {
let temp = elements[i].style.height;
elements[i].style.height = elements[largest].style.height;
elements[largest].style.height = temp;
elements[i].style.backgroundColor = '#CAD6E3';
elements[largest].style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
elements[i].style.backgroundColor = '';
elements[largest].style.backgroundColor = '';
await heapify(n, largest);
}
}
for (let i = Math.floor(len / 2) - 1; i >= 0; i--) {
await heapify(len, i);
}
for (let i = len - 1; i > 0; i--) {
let temp = elements[0].style.height;
elements[0].style.height = elements[i].style.height;
elements[i].style.height = temp;
elements[0].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
elements[0].style.backgroundColor = '';
await heapify(i, 0);
}
}
//complete
async function shellSort() {
let elements = document.querySelectorAll('.element');
let len = elements.length;
for (let gap = Math.floor(len / 2); gap > 0; gap = Math.floor(gap / 2)) {
for (let i = gap; i < len; i++) {
let temp = elements[i].style.height;
let j = i;
while (j >= gap && parseInt(elements[j - gap].style.height) > parseInt(temp)) {
elements[j].style.height = elements[j - gap].style.height;
elements[j - gap].style.backgroundColor = '#CAD6E3';
elements[j].style.backgroundColor = '#CAD6E3';
await new Promise(resolve => setTimeout(resolve, 500));
elements[j - gap].style.backgroundColor = '';
elements[j].style.backgroundColor = '';
j -= gap;
}
elements[j].style.height = temp;
elements[j].style.backgroundColor = '#85C1E9';
await new Promise(resolve => setTimeout(resolve, 500));
elements[j].style.backgroundColor = '';
}
}
}