-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinsertion.js
42 lines (38 loc) · 1.45 KB
/
insertion.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
async function insertion() {
const e = document.querySelectorAll(".bar");
e[0].style.background = "var(--bars-sorted-color)";
for (let i = 1; i < e.length; i++) {
let j = i - 1;
let x = e[i].style.height;
e[i].style.background = "var(--bars-sorting-blue-color)";
await waitForMe(delay);
while (j > -1 && parseInt(e[j].style.height) > parseInt(x)) {
e[j].style.background = "var(--bars-sorting-blue-color)";
e[j + 1].style.height = e[j].style.height;
j--;
await waitForMe(delay);
for (let k = i; k >= 0; k--) e[k].style.background = "var(--bars-sorted-color)";
}
e[j + 1].style.height = x;
e[i].style.background = "var(--bars-sorted-color)";
}
}
const ins = document.querySelector(".insertionSort");
ins.addEventListener("click", async function () {
disableSortBtn();
disableSizeSlider();
disableNewArrayBtn();
diablePerformanceBtn()
const startTime = performance.now();
await insertion();
const endTime = performance.now();
console.log(`Call to doSomething took ${endTime - startTime} milliseconds.`);
var text = document.getElementById("content");
const p = document.querySelectorAll(".bar");
var time = (endTime - startTime)/1000;
text.textContent = "Array Consist of "+ p.length+" elements and got sorted in "+time.toPrecision(2)+" seconds with a Speed Delay of " + delay+" miliseconds.";
enableSortBtn();
enablePerformanceBtn();
enableNewArrayBtn();
enableSizeSlider();
});