-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.js
More file actions
61 lines (59 loc) · 1.96 KB
/
selectionSort.js
File metadata and controls
61 lines (59 loc) · 1.96 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
54
55
56
57
58
59
60
61
async function selection(){
const ele = document.querySelectorAll(".bar");
for(let i = 0; i < ele.length; i++){
if(hasPressedStop==true){
return;
}
let min_index = i;
// Change color of the bar being compared
ele[i].style.background = 'lightgreen';
for(let j = i+1; j < ele.length; j++){
if(hasPressedStop==true){
return;
}
// Change color of current bar
ele[j].style.background = 'cyan';
await delayTime(delay);
if(hasPressedStop==true){
return;
}
if(parseInt(ele[j].style.height) < parseInt(ele[min_index].style.height)){
if(min_index !== i){
// new min_index is found so change prev min_index color back to normal
ele[min_index].style.background = '#e43f5a';
}
min_index = j;
}
else{
// if the currnent comparision is more than min_index change is back to normal
ele[j].style.background = '#e43f5a';
}
}
await delayTime(delay);
if(hasPressedStop==true){
return;
}
swap(ele[min_index], ele[i]);
// change the min element index back to normal as it is swapped
ele[min_index].style.background = '#e43f5a';
// change the sorted elements color to green
ele[i].style.background = 'green';
}
}
const selectionSortbtn = document.querySelector(".selectionSort");
selectionSortbtn.addEventListener('click', async function(){
hasPressedStop = false;
disableSortingBtn();
disableSizeSlider();
disableNewArrayBtn();
enableStopSortingBtn();
await selection();
if(hasPressedStop==true){
disableSpeedSlider();
} else {
enableSortingBtn();
enableSizeSlider();
}
enableNewArrayBtn();
disableStopSortingBtn();
});