-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (55 loc) · 2 KB
/
script.js
File metadata and controls
74 lines (55 loc) · 2 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
const sizeRange = document.getElementById("sizeRange");
const sizeValue = document.getElementById("sizeValue");
sizeRange.addEventListener("input", () => {
sizeValue.textContent = sizeRange.value;
generateArray(); // regenerate on size change
});
function generateArray() {
array = [];
const visualizerElement = document.getElementById("visualizer");
const visualizerWidth = visualizerElement.offsetWidth;
visualizerElement.innerHTML = "";
const barCount = parseInt(document.getElementById("sizeRange").value);
const barWidth = Math.max(2, Math.floor(visualizerWidth / barCount));
for (let i = 0; i < barCount; i++) {
const visualizerHeight = visualizerElement.offsetHeight;
const maxBarHeight = visualizerHeight - 20; // leave padding space
const value = Math.floor(Math.random() * maxBarHeight) + 10;
array.push(value);
const bar = document.createElement("div");
bar.classList.add("bar");
bar.style.height = `${value}px`;
bar.style.width = `${barWidth}px`;
visualizerElement.appendChild(bar);
}
console.log("Bar count:", barCount, "| Bar width:", barWidth);
}
function getSpeed() {
const range = document.getElementById("speedRange").value;
return 200 - range * 1.8; // Speed scaling
}
function startSort() {
const algo = document.getElementById("algorithm-select").value;
const bars = document.querySelectorAll(".bar");
const speed = getSpeed();
if (algo === "bubble") {
bubbleSort([...array], bars, speed);
} else if (algo === "selection") {
selectionSort([...array], bars, speed);
} else if (algo === "insertion") {
insertionSort([...array], bars, speed);
} else if (algo === "merge") {
mergeSort([...array], bars, speed);
} else if (algo === "quick") {
quickSort([...array], bars, speed);
}
}
// Load default theory
document.getElementById("algorithm-select").addEventListener("change", (e) => {
loadTheory(e.target.value);
});
// Initial load
window.onload = () => {
generateArray();
loadTheory("bubble");
};