forked from prabhat2027/Sorting-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (64 loc) · 1.72 KB
/
script.js
File metadata and controls
64 lines (64 loc) · 1.72 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
/* JS FILE: script.js */
let bars = [];
function generateBars() {
const container = document.getElementById("bars");
container.innerHTML = "";
bars = [];
for (let i = 0; i < 20; i++) {
let value = Math.floor(Math.random() * 100) + 10;
let bar = document.createElement("div");
bar.style.height = value + "px";
bar.classList.add("bar");
bars.push({ element: bar, value });
container.appendChild(bar);
}
}
async function bubbleSort() {
for (let i = 0; i < bars.length - 1; i++) {
for (let j = 0; j < bars.length - i - 1; j++) {
if (bars[j].value > bars[j + 1].value) {
await swap(j, j + 1);
}
}
}
}
async function selectionSort() {
for (let i = 0; i < bars.length; i++) {
let minIdx = i;
for (let j = i + 1; j < bars.length; j++) {
if (bars[j].value < bars[minIdx].value) {
minIdx = j;
}
}
await swap(i, minIdx);
}
}
async function insertionSort() {
for (let i = 1; i < bars.length; i++) {
let key = bars[i];
let j = i - 1;
while (j >= 0 && bars[j].value > key.value) {
bars[j + 1] = bars[j];
j--;
}
bars[j + 1] = key;
updateBars();
await sleep(100);
}
}
function updateBars() {
const container = document.getElementById("bars");
container.innerHTML = "";
bars.forEach(bar => container.appendChild(bar.element));
}
async function swap(i, j) {
await sleep(100);
let temp = bars[i];
bars[i] = bars[j];
bars[j] = temp;
updateBars();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
generateBars();