-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.js
157 lines (144 loc) · 4.94 KB
/
heap.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
let arr = [];
for(let i=0;i<10;i++)
{
arr.push(Math.floor(Math.random() * 200) + 20);
}
let parentDiv = document.getElementsByClassName('parentDiv');
let btn = document.getElementsByClassName('startbtn');
let speed_slider= document.getElementById('speed');
let speed = Number(speed_slider.value);
let array_size = document.getElementById('array-size');
let i=0;
arr.forEach(e=>{
let innerDiv = document.createElement('div');
innerDiv.innerText = e;
innerDiv.style.color = 'white';
innerDiv.style.height = (e + 'px');
innerDiv.style.backgroundColor = 'black';
innerDiv.style.margin='2px';
innerDiv.style.width = '50px';
innerDiv.setAttribute('id', 'elem'+i);
i++;
innerDiv.classList.add('innerDiv');
parentDiv[0].appendChild(innerDiv);
});
btn[0].addEventListener("click", ()=>myFunction(arr));
const sleep = (time) => {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function heapSort(arr){
let n = arr.length;
for(let i=Math.floor(n/2)-1; i>=0; i--){
parentDiv[0].childNodes[i].style.backgroundColor = 'red';
await heapify(arr,n,i);
await sleep(speed);
parentDiv[0].childNodes[i].style.backgroundColor = 'black';
}
for(let i=n-1; i>=0; i--){
parentDiv[0].childNodes[0].style.backgroundColor = 'red';
parentDiv[0].childNodes[i].style.backgroundColor = 'red';
swapNumber(arr,0,i);
swapColorHeight(0,i);
await sleep(speed);
parentDiv[0].childNodes[0].style.backgroundColor = 'black';
parentDiv[0].childNodes[i].style.backgroundColor = 'black';
await heapify(arr,i,0);
await sleep(speed);
parentDiv[0].childNodes[i].style.backgroundColor = 'green';
}
}
async function heapify(arr,n,i){
let largest = i;
let l = 2*i+1;
let r = 2*i+2;
if(l<n && arr[l]>arr[largest]){
largest = l;
}
if(r<n && arr[r]>arr[largest]){
largest = r;
}
if(largest!=i){
parentDiv[0].childNodes[i].style.backgroundColor = 'red';
parentDiv[0].childNodes[largest].style.backgroundColor = 'red';
swapNumber(arr,i,largest);
swapColorHeight(i,largest);
await heapify(arr,n,largest);
await sleep(speed);
parentDiv[0].childNodes[i].style.backgroundColor = 'black';
parentDiv[0].childNodes[largest].style.backgroundColor = 'black';
}
}
function swapNumber(arr,i){
let temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
}
function swapColorHeight(i){
let a = 'elem'+i;
let b = 'elem'+0;
let e1 = document.getElementById(a);
let e2 = document.getElementById(b);
let bg1 = e1.style.backgroundColor;
let bg2 = e2.style.backgroundColor;
let h1 = e1.clientHeight;
let h2 = e2.clientHeight;
e1.style.backgroundColor = bg2;
let temp = e1.innerText;
e1.innerText = e2.innerText;
e2.innerText = temp;
e2.style.backgroundColor = bg1;
e1.style.height = h2 + 'px';
e2.style.height = h1 + 'px';
}
function swapNumber(arr,i,largest){
let temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
}
function swapColorHeight(i,largest){
let a = 'elem'+i;
let b = 'elem'+largest;
let e1 = document.getElementById(a);
let e2 = document.getElementById(b);
let bg1 = e1.style.backgroundColor;
let bg2 = e2.style.backgroundColor;
let h1 = e1.clientHeight;
let h2 = e2.clientHeight;
e1.style.backgroundColor = bg2;
let temp = e1.innerText;
e1.innerText = e2.innerText;
e2.innerText = temp;
e2.style.backgroundColor = bg1;
e1.style.height = h2 + 'px';
e2.style.height = h1 + 'px';
}
async function myFunction(arr){
array_size.disabled = true;
await heapSort(arr);
array_size.disabled = false;
}
speed_slider.addEventListener('input', function () {
speed = Number(speed_slider.value);
}, false);
array_size.addEventListener('input', function () {
parentDiv[0].innerHTML = '';
arr = [];
let size = Number(array_size.value);
for(let i = 0; i < size; i++) {
arr.push(Math.floor(Math.random() * 200) + 20);
}
let i=0;
arr.forEach(e=>{
let innerDiv = document.createElement('div');
innerDiv.innerText = e;
innerDiv.style.color = 'white';
innerDiv.style.height = (e + 'px');
innerDiv.style.backgroundColor = 'black';
innerDiv.style.margin='2px';
innerDiv.style.width = '50px';
innerDiv.setAttribute('id', 'elem'+i);
i++;
innerDiv.classList.add('innerDiv');
parentDiv[0].appendChild(innerDiv);
});
}, false);