Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
476 lines (370 loc) · 10.7 KB
/
Copy pathscript.js
File metadata and controls
476 lines (370 loc) · 10.7 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
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
"use strict";
/* ==========================================
GET HTML ELEMENTS
========================================== */
const container = document.getElementById("arrayContainer");
const algorithm = document.getElementById("algorithm");
const sizeSlider = document.getElementById("size");
const speedSlider = document.getElementById("speed");
const sizeValue = document.getElementById("sizeValue");
const speedValue = document.getElementById("speedValue");
const generateBtn = document.getElementById("generateBtn");
const sortBtn = document.getElementById("sortBtn");
const comparisonsText = document.getElementById("comparisons");
const swapsText = document.getElementById("swaps");
const statusText = document.getElementById("status");
/* ==========================================
VARIABLES
========================================== */
let array = [];
let comparisons = 0;
let swaps = 0;
let isSorting = false;
/* ==========================================
GENERATE ARRAY
========================================== */
function generateArray() {
if (isSorting) return;
array = [];
const size = Number(sizeSlider.value);
for (let i = 0; i < size; i++) {
const value = Math.floor(Math.random() * 90) + 10;
array.push(value);
}
comparisons = 0;
swaps = 0;
updateStatistics();
drawArray();
}
/* ==========================================
DRAW ARRAY
========================================== */
function drawArray() {
container.innerHTML = "";
if (array.length === 0) return;
const max = Math.max(...array);
for (let i = 0; i < array.length; i++) {
const bar = document.createElement("div");
bar.className = "bar";
const height = (array[i] / max) * 100;
bar.style.height = height + "%";
container.appendChild(bar);
}
}
/* ==========================================
UPDATE BAR
========================================== */
function updateBar(index) {
const bars = container.children;
if (!bars[index]) return;
const max = Math.max(...array);
bars[index].style.height = (array[index] / max) * 100 + "%";
}
/* ==========================================
DELAY
========================================== */
function sleep(milliseconds) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}
function getDelay() {
const speed = Number(speedSlider.value);
// Slider: 1 = slow, 100 = fast
return 510 - speed * 5;
}
/* ==========================================
WAIT
========================================== */
async function wait() {
await sleep(getDelay());
}
/* ==========================================
COMPARE
========================================== */
async function compare(i, j) {
const bars = container.children;
comparisons++;
updateStatistics();
if (bars[i]) bars[i].classList.add("comparing");
if (bars[j]) bars[j].classList.add("comparing");
await wait();
if (bars[i]) bars[i].classList.remove("comparing");
if (bars[j]) bars[j].classList.remove("comparing");
}
/* ==========================================
SWAP
========================================== */
async function swap(i, j) {
const bars = container.children;
if (bars[i]) bars[i].classList.add("swapping");
if (bars[j]) bars[j].classList.add("swapping");
await wait();
const temp = array[i];
array[i] = array[j];
array[j] = temp;
updateBar(i);
updateBar(j);
swaps++;
updateStatistics();
await wait();
if (bars[i]) bars[i].classList.remove("swapping");
if (bars[j]) bars[j].classList.remove("swapping");
}
/* ==========================================
MARK SORTED
========================================== */
function markSorted(index) {
const bars = container.children;
if (bars[index]) bars[index].classList.add("sorted");
}
async function markAllSorted() {
const bars = container.children;
for (let i = 0; i < bars.length; i++) {
bars[i].classList.remove("comparing");
bars[i].classList.remove("swapping");
bars[i].classList.add("sorted");
await sleep(10);
}
}
/* ==========================================
BUBBLE SORT
========================================== */
async function bubbleSort() {
const n = array.length;
for (let i = 0; i < n - 1; i++) {
let changed = false;
for (let j = 0; j < n - i - 1; j++) {
await compare(j, j + 1);
if (array[j] > array[j + 1]) {
await swap(j, j + 1);
changed = true;
}
}
markSorted(n - i - 1);
if (!changed) break;
}
if (n > 0) markSorted(0);
}
/* ==========================================
SELECTION SORT
========================================== */
async function selectionSort() {
const n = array.length;
for (let i = 0; i < n - 1; i++) {
let min = i;
for (let j = i + 1; j < n; j++) {
await compare(min, j);
if (array[j] < array[min]) {
min = j;
}
}
if (min !== i) {
await swap(i, min);
}
markSorted(i);
}
if (n > 0) markSorted(n - 1);
}
/* ==========================================
INSERTION SORT
========================================== */
async function insertionSort() {
const n = array.length;
for (let i = 1; i < n; i++) {
let j = i;
while (j > 0) {
await compare(j - 1, j);
if (array[j - 1] > array[j]) {
await swap(j - 1, j);
j--;
} else {
break;
}
}
}
await markAllSorted();
}
/* ==========================================
MERGE SORT
========================================== */
async function mergeSort(left, right) {
if (left >= right) return;
const middle = Math.floor((left + right) / 2);
await mergeSort(left, middle);
await mergeSort(middle + 1, right);
await merge(left, middle, right);
}
async function merge(left, middle, right) {
const temp = [];
let i = left;
let j = middle + 1;
while (i <= middle && j <= right) {
await compare(i, j);
if (array[i] <= array[j]) {
temp.push(array[i]);
i++;
} else {
temp.push(array[j]);
j++;
}
}
while (i <= middle) {
temp.push(array[i]);
i++;
}
while (j <= right) {
temp.push(array[j]);
j++;
}
for (let k = 0; k < temp.length; k++) {
array[left + k] = temp[k];
updateBar(left + k);
await wait();
}
}
/* ==========================================
QUICK SORT
========================================== */
async function quickSort(low, high) {
if (low >= high) return;
const pivot = await partition(low, high);
await quickSort(low, pivot - 1);
await quickSort(pivot + 1, high);
}
async function partition(low, high) {
const pivot = array[high];
let i = low - 1;
for (let j = low; j < high; j++) {
await compare(j, high);
if (array[j] <= pivot) {
i++;
if (i !== j) {
await swap(i, j);
}
}
}
if (i + 1 !== high) {
await swap(i + 1, high);
}
return i + 1;
}
/* ==========================================
HEAP SORT
========================================== */
async function heapSort() {
const n = array.length;
// Build Max Heap
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
await heapify(n, i);
}
// Extract maximum
for (let i = n - 1; i > 0; i--) {
await swap(0, i);
markSorted(i);
await heapify(i, 0);
}
if (n > 0) markSorted(0);
}
async function heapify(n, i) {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n) {
await compare(left, largest);
if (array[left] > array[largest]) {
largest = left;
}
}
if (right < n) {
await compare(right, largest);
if (array[right] > array[largest]) {
largest = right;
}
}
if (largest !== i) {
await swap(i, largest);
await heapify(n, largest);
}
}
/* ==========================================
START SORTING
========================================== */
async function startSorting() {
if (isSorting) return;
isSorting = true;
// Disable controls
generateBtn.disabled = true;
sortBtn.disabled = true;
algorithm.disabled = true;
sizeSlider.disabled = true;
statusText.textContent = "Sorting...";
comparisons = 0;
swaps = 0;
updateStatistics();
try {
switch (algorithm.value) {
case "bubble":
await bubbleSort();
break;
case "selection":
await selectionSort();
break;
case "insertion":
await insertionSort();
break;
case "merge":
await mergeSort(0, array.length - 1);
await markAllSorted();
break;
case "quick":
await quickSort(0, array.length - 1);
await markAllSorted();
break;
case "heap":
await heapSort();
break;
default:
// unknown algorithm
break;
}
statusText.textContent = "Completed";
} catch (error) {
console.error(error);
statusText.textContent = "Error";
alert("An error occurred. Press F12 and check Console.");
}
isSorting = false;
// Enable controls
generateBtn.disabled = false;
sortBtn.disabled = false;
algorithm.disabled = false;
sizeSlider.disabled = false;
}
/* ==========================================
UPDATE STATISTICS
========================================== */
function updateStatistics() {
comparisonsText.textContent = comparisons;
swapsText.textContent = swaps;
sizeValue.textContent = sizeSlider.value;
speedValue.textContent = speedSlider.value;
}
/* ==========================================
EVENT LISTENERS
========================================== */
generateBtn.addEventListener("click", generateArray);
sortBtn.addEventListener("click", startSorting);
sizeSlider.addEventListener("input", function () {
if (!isSorting) {
sizeValue.textContent = this.value;
generateArray();
}
});
speedSlider.addEventListener("input", function () {
speedValue.textContent = this.value;
});
/* ==========================================
INITIAL ARRAY
========================================== */
generateArray();