-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.js
115 lines (100 loc) · 3.82 KB
/
sorting.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
"use strict";
const barsEl = document.getElementById("bars");
const sizeSlider = document.getElementById("size");
// const barsRect = barsEl.getBoundingClientRect();
const btnRandomArray = document.getElementById("random-array");
const btnReversedArray = document.getElementById("reversed-array");
const btnSortedArray = document.getElementById("sorted-array");
const btnReload = document.getElementById("reload");
// const barsWidth = barsRect.width;
// const barsHeight = barsRect.height;
const barsWidth = barsEl.offsetWidth;
const barsHeight = barsEl.offsetHeight;
barsEl.innerHTML = "";
let bars = [];
const sortedArray = function () {
bars = [];
for (let i = 1; i <= sizeSlider.value; ++i) {
bars.push(i);
}
plantArray()
}
const reversedArray = function () {
bars = [];
for (let i = sizeSlider.value; i > 0; --i) {
bars.push(i);
}
plantArray()
}
const randomArray = function () {
bars = [];
for (let i = 0; i < Number(sizeSlider.value); ++i) {
bars.push(Math.floor(Math.random() * 99) + 1);
}
plantArray()
}
const plantArray = function () {
tempEnable();
barsEl.innerHTML = "";
const newWidth = (Number(barsWidth) * 0.96) / Number(sizeSlider.value);
for (let i = 0; i < Number(sizeSlider.value); ++i) {
const divEl = document.createElement("div");
divEl.classList.add("bar-prop");
divEl.style.display = "inline-block";
divEl.style.width = `${newWidth * 0.94}px`;
divEl.style.height = `${Number(bars[i]) * (0.94)}%`;
divEl.style.margin = `0 ${0.05 * newWidth}px 0`;
divEl.style.marginBottom = `${Number(barsHeight * 0.05)}px`;
divEl.style.position = "relative";
divEl.setAttribute("id", `bar-${i}`);
const textEl = document.createElement("div");
textEl.classList.add("bottom");
// textEl.style.height = `${Number(newWidth * 0.5)}px`;
textEl.innerText = ".";
divEl.appendChild(textEl);
barsEl.appendChild(divEl);
}
};
function waitforme(millisec) {
return new Promise((resolve) => {
setTimeout(() => {
resolve("");
}, Number(410 - millisec));
});
}
const swap = function (bar1, bar2) {
const bar1Height = Number(bar1.style.height.slice(0, -1));
const bar2Height = Number(bar2.style.height.slice(0, -1));
const temp = bar1Height;
const t2 = bar1.children[0].innerText;
bar1.style.height = `${bar2Height}%`;
bar1.children[0].innerText = bar2.children[0].innerText;
bar2.style.height = `${temp}%`;
bar2.children[0].innerText = t2;
};
const tempDisable = function () {
document.getElementById("random-array").classList.add("disabled");
document.getElementById("reversed-array").classList.add("disabled");
document.getElementById("sorted-array").classList.add("disabled");
document.getElementById("size").classList.add("disabled");
document.getElementById("bubble").classList.add("disabled");
document.getElementById("merge").classList.add("disabled");
document.getElementById("quick").classList.add("disabled");
document.getElementById("insertion").classList.add("disabled");
document.getElementById("selection").classList.add("disabled");
};
const tempEnable = function () {
document.getElementById("random-array").classList.remove("disabled");
document.getElementById("reversed-array").classList.remove("disabled");
document.getElementById("sorted-array").classList.remove("disabled");
document.getElementById("size").classList.remove("disabled");
document.getElementById("bubble").classList.remove("disabled");
document.getElementById("merge").classList.remove("disabled");
document.getElementById("quick").classList.remove("disabled");
document.getElementById("insertion").classList.remove("disabled");
document.getElementById("selection").classList.remove("disabled");
};
btnRandomArray.addEventListener("click", randomArray);
btnReversedArray.addEventListener("click", reversedArray);
btnSortedArray.addEventListener("click", sortedArray);
btnReload.addEventListener("click", () => location.reload());