const container = document.querySelector(
".data-container"
);
function
generatebars(num = 20) {
for
(let i = 0; i < num; i += 1) {
const value = Math.floor(Math.random() * 100) + 1;
const bar = document.createElement(
"div"
);
bar.classList.add(
"bar"
);
bar.style.height = `${value * 3}px`;
bar.style.transform = `translateX(${i * 30}px)`;
const barLabel = document.createElement(
"label"
);
barLabel.classList.add(
"bar__id"
);
barLabel.innerHTML = value;
bar.appendChild(barLabel);
container.appendChild(bar);
}
}
function
calculate_gap(gap) {
gap = parseInt((gap * 10) / 13, 10);
if
(gap < 1)
return
1;
return
gap;
}
async
function
CombSort(delay = 600) {
let bars = document.querySelectorAll(
".bar"
);
let gap = 20;
let swapped =
true
;
while
(gap != 1 || swapped ==
true
) {
gap = calculate_gap(gap);
swapped =
false
;
for
(let i = 0; i < 20 - gap; i++) {
let value1 = parseInt(bars[i].childNodes[0].innerHTML);
let value2 = parseInt(bars[i + gap].childNodes[0].innerHTML);
if
(value1 > value2) {
bars[i].style.backgroundColor =
"red"
;
bars[i + gap].style.backgroundColor =
"red"
;
let temp1 = bars[i].style.height;
let temp2 = bars[i].childNodes[0].innerText;
await
new
Promise((resolve) =>
setTimeout(() => {
resolve();
}, 300)
);
bars[i].style.height = bars[i + gap].style.height;
bars[i].childNodes[0].innerText = bars[i + gap].childNodes[0].innerText;
bars[i + gap].style.height = temp1;
bars[i + gap].childNodes[0].innerText = temp2;
swapped =
true
;
await
new
Promise((resolve) =>
setTimeout(() => {
resolve();
}, 300)
);
}
bars[i].style.backgroundColor =
"rgb(0, 183, 255)"
;
bars[i + gap].style.backgroundColor =
"rgb(0, 183, 255)"
;
await
new
Promise((resolve) =>
setTimeout(() => {
resolve();
}, 300)
);
}
}
for
(let x = 0; x < 20; x++) {
bars[x].style.backgroundColor =
"rgb(49, 226, 13)"
;
}
document.getElementById(
"Button1"
).disabled =
false
;
document.getElementById(
"Button1"
).style.backgroundColor =
"#6f459e"
;
document.getElementById(
"Button2"
).disabled =
false
;
document.getElementById(
"Button2"
).style.backgroundColor =
"#6f459e"
;
}
generatebars();
function
generate() {
window.location.reload();
}
function
disable() {
document.getElementById(
"Button1"
).disabled =
true
;
document.getElementById(
"Button1"
).style.backgroundColor =
"#d8b6ff"
;
document.getElementById(
"Button2"
).disabled =
true
;
document.getElementById(
"Button2"
).style.backgroundColor =
"#d8b6ff"
;
}