-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.js
More file actions
46 lines (44 loc) · 1.74 KB
/
selection.js
File metadata and controls
46 lines (44 loc) · 1.74 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
async function selection(){
console.log('In selection()');
const ele = document.querySelectorAll(".bar");
for(let i = 0; i < ele.length; i++){
console.log('In ith loop');
let min_index = i;
// Change color of the position to swap with the next min
ele[i].style.background = 'blue';
for(let j = i+1; j < ele.length; j++){
console.log('In jth loop');
// Change color for the current comparision (in consideration for min_index)
ele[j].style.background = 'red';
await waitforme(delay);
if(parseInt(ele[j].style.height) < parseInt(ele[min_index].style.height)){
console.log('In if condition height comparision');
if(min_index !== i){
// new min_index is found so change prev min_index color back to normal
ele[min_index].style.background = 'cyan';
}
min_index = j;
}
else{
// if the currnent comparision is more than min_index change is back to normal
ele[j].style.background = 'cyan';
}
}
await waitforme(delay);
swap(ele[min_index], ele[i]);
// change the min element index back to normal as it is swapped
ele[min_index].style.background = 'cyan';
// change the sorted elements color to green
ele[i].style.background = 'green';
}
}
const selectionSortbtn = document.querySelector(".selectionSort");
selectionSortbtn.addEventListener('click', async function(){
disableSortingBtn();
disableSizeSlider();
disableNewArrayBtn();
await selection();
enableSortingBtn();
enableSizeSlider();
enableNewArrayBtn();
});