-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.js
More file actions
109 lines (81 loc) · 2.99 KB
/
sorting.js
File metadata and controls
109 lines (81 loc) · 2.99 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var array = [];
generateBars();
// ***js required to change the value of range type input for no of bars
document.querySelector("#noBarsInput").addEventListener('change',function() {
this.setAttribute('value',this.value); // to change
generateBars();
});
// ***js required to change the value of range type input for speed of animation
document.querySelector("#speedInput").addEventListener('change',function() {
this.setAttribute('value',this.value); // to change
});
const newArrayBtn = document.querySelector(".newArray");
newArrayBtn.addEventListener("click",generateBars);
const bubbleBtn = document.querySelector(".bubble");
bubbleBtn.addEventListener("click",bubbleSort);
const selectionBtn = document.querySelector(".selection");
selectionBtn.addEventListener("click",selectionSort);
const insertionBtn = document.querySelector(".insertion");
insertionBtn.addEventListener("click",insertionSort);
const mergeBtn = document.querySelector(".merge");
mergeBtn.addEventListener("click",mergeSort);
const quickBtn = document.querySelector(".quick");
quickBtn.addEventListener("click",quickSort);
// Functions
function findRandom(){
var n = Math.random();
n = n * 30; // height will range from 1 to 30
n = Math.floor(n) + 1;
return n;
}
function generateBars(){
array = [];
// To remove all existing divs made for bars from html file
var existbars = document.querySelectorAll("#bars div");
existbars.forEach(function(eachdiv){
eachdiv.remove();
})
var noBars = Number(document.querySelector("#noBarsInput").getAttribute("value"));
console.log(noBars);
for(var i=0;i<noBars;i++){
array.push(findRandom());
// creating sub divs in div having id as bars
var newdiv = document.createElement("DIV");
document.querySelector("#bars").appendChild(newdiv);
document.querySelectorAll("#bars div")[i].classList.add("bar");
document.querySelectorAll("#bars div")[i].style.height=array[i]+"em";
}
}
function swap(f,l){
var temp = array[f];
array[f] = array[l];
array[l] = temp;
}
async function swapCssHeight(el1,el2){
el1.style.background="#F55C47"; // animation for elements to swap
el2.style.background="#F55C47";
await delay();
const style1 = window.getComputedStyle(el1);
const style2 = window.getComputedStyle(el2);
const transform1 = style1.getPropertyValue("height");
const transform2 = style2.getPropertyValue("height");
el1.style.height = transform2;
el2.style.height = transform1;
el1.style.background="#4AA96C";
el2.style.background="#4AA96C";
}
async function animation(el1,el2){
el1.style.background="#9FE6A0"; // animation for elements to swap
el2.style.background="#9FE6A0";
await delay();
el1.style.background="#4AA96C";
el2.style.background="#4AA96C";
}
function delay() {
var speed = document.querySelector("#speedInput").getAttribute("value");
return new Promise(resolve => {
setTimeout(function(){
resolve();
}, speed);
});
}