-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.js
77 lines (64 loc) · 2.59 KB
/
selection.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
"use strict";
const selectionSort = async function () {
document.getElementById("selection").classList.toggle("active-btn");
tempDisable();
const barsEl = document.getElementById("bars");
const sz = Number(barsEl.childElementCount);
for (let i = 0; i < sz; ++i) {
let barDefaultColor;
let barDefaultBorderColor = barsEl.children[0].style.borderColor;
let mxInd = 0;
barsEl.children[0].children[0].style.backgroundColor = "#FF2171";
barsEl.children[0].style.borderColor = "#FF2171";
for (let j = 1; j < sz - i; ++j) {
const barCrr = barsEl.children[j];
const barMx = barsEl.children[mxInd];
barDefaultColor = barCrr.style.backgroundColor;
const barCrrHeight = Number(barCrr.style.height.slice(0, -1));
const barMxHeight = Number(barMx.style.height.slice(0, -1));
await waitforme(Number(speedSlider.value));
let chng = false;
let tmpSave;
if (barCrrHeight > barMxHeight) {
chng = true;
tmpSave = mxInd;
mxInd = j;
}
barCrr.style.backgroundColor = "red";
barMx.style.backgroundColor = "red";
await waitforme(Number(speedSlider.value));
if (chng) {
barsEl.children[tmpSave].children[0].style.removeProperty(
"background-color"
);
barsEl.children[tmpSave].style.borderColor =
barDefaultBorderColor;
barsEl.children[mxInd].children[0].style.backgroundColor =
"#FF2171";
barsEl.children[mxInd].style.borderColor = "#FF2171";
}
barCrr.style.backgroundColor = barDefaultColor;
barMx.style.backgroundColor = barDefaultColor;
}
const temp = barsEl.children[sz - i - 1].style.height;
const t2 = barsEl.children[sz - i - 1].children[0].innerText;
barsEl.children[sz - i - 1].style.height =
barsEl.children[mxInd].style.height;
barsEl.children[sz - i - 1].children[0].innerText =
barsEl.children[mxInd].children[0].innerText;
barsEl.children[mxInd].style.height = temp;
barsEl.children[mxInd].children[0].innerText = t2;
barsEl.children[sz - i - 1].style.backgroundColor = "green";
barsEl.children[mxInd].children[0].style.removeProperty(
"background-color"
);
barsEl.children[mxInd].style.borderColor = barDefaultBorderColor;
}
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("selection").classList.toggle("active-btn");
};
const btnSelectionSort = document.getElementById("selection");
btnSelectionSort.addEventListener("click", selectionSort);