-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.js
More file actions
53 lines (49 loc) · 1.42 KB
/
insertionSort.js
File metadata and controls
53 lines (49 loc) · 1.42 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
async function insertion(){
const ele = document.querySelectorAll(".bar");
ele[0].style.background = 'green';
for(let i = 1; i < ele.length; i++){
if(hasPressedStop==true){
return;
}
let j = i - 1;
let key = ele[i].style.height;
ele[i].style.background = 'blue';
await delayTime(delay);
if(hasPressedStop==true){
return;
}
while(j >= 0 && (parseInt(ele[j].style.height) > parseInt(key))){
if(hasPressedStop==true){
return;
}
ele[j].style.background = 'blue';
ele[j + 1].style.height = ele[j].style.height;
j--;
await delayTime(delay);
if(hasPressedStop==true){
return;
}
for(let k = i; k >= 0; k--){
ele[k].style.background = 'green';
}
}
ele[j + 1].style.height = key;
ele[i].style.background = 'green';
}
}
const inSortbtn = document.querySelector(".insertionSort");
inSortbtn.addEventListener('click', async function(){
disableSortingBtn();
disableSizeSlider();
disableNewArrayBtn();
enableStopSortingBtn();
await insertion();
if(hasPressedStop==true){
disableSpeedSlider();
} else {
enableSortingBtn();
enableSizeSlider();
}
enableNewArrayBtn();
disableStopSortingBtn();
});