-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
241 lines (199 loc) · 6.4 KB
/
script.js
File metadata and controls
241 lines (199 loc) · 6.4 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
let array =[];
let delay=0;
let allBars;
function updateArraySize(n)
{
document.getElementById("sizeValue").innerText = n;
generateArray(n);
}
let speed = 3;
function updateSortingSpeed(val) {
document.getElementById("sortingSpeedValue").innerText = val;
speed = val;
}
function generateArray(n)
{
array = [];
for(let i=0; i<n; i++)
{
array.push(Math.floor(Math.random() * 100) + 1);
}
displayArray(array);
}
function displayArray(array)
{
let container = document.querySelector(".bars-container");
container.innerHTML = "";
array.forEach((value) => {
let bar = document.createElement('div');
bar.classList.add('bar');
bar.style.height = (value*3) + "px";
container.appendChild(bar);
});
}
function bubbleSort()
{
allBars = document.querySelectorAll(".bar");
let temp;
delay = 0;
let n=allBars.length;
for(let i=0; i<n-1; i++)
{
for(let j=0; j<n-1-i; j++)
{
((j) => {
setTimeout(() => {
// highlight bars
allBars[j].classList.add("compare");
allBars[j+1].classList.add("compare");
}, delay);
setTimeout(() => {
// read heights NOW, after delay
let height1 = parseInt(allBars[j].style.height);
let height2 = parseInt(allBars[j+1].style.height);
// swap if needed
if(height1 > height2){
let temp = allBars[j].style.height;
allBars[j].style.height = allBars[j+1].style.height;
allBars[j+1].style.height = temp;
}
}, delay+250);
setTimeout(() => {
allBars[j].classList.remove("compare");
allBars[j+1].classList.remove("compare");
}, delay+500);
})(j);
delay += 1000/speed;
}
setTimeout(() => {
allBars[n-1-i].style.backgroundColor = "green";
}, delay);
}
setTimeout(() => {
for(let k = 0; k < n; k++){
allBars[k].style.backgroundColor = '#0047AB';
}
}, delay);
}
window.onload = () => {
let defaultSize = document.getElementById("sizeSlider").value;
generateArray(defaultSize);
};
let animations = [];
function startMergeSort() {
allBars = document.querySelectorAll(".bar");
let array = Array.from(allBars).map(bar => parseInt(bar.style.height));
animations = [];
delay = 0;
generateMergeSortAnimations(array, 0, array.length - 1);
playAnimations(array);
}
// Generate all animations for merge sort
function generateMergeSortAnimations(array, l, r) {
if (l >= r) return;
const mid = Math.floor((l + r) / 2);
generateMergeSortAnimations(array, l, mid);
generateMergeSortAnimations(array, mid + 1, r);
merge(array, l, mid, r);
}
function merge(array, l, mid, r) {
let i = l;
let j = mid + 1;
const temp = [];
while (i <= mid && j <= r) {
animations.push({ type: 'compare', indices: [i, j] });
if (array[i] <= array[j]) {
temp.push(array[i]);
animations.push({ type: 'overwrite', index: l + temp.length - 1, height: array[i] });
i++;
} else {
temp.push(array[j]);
animations.push({ type: 'overwrite', index: l + temp.length - 1, height: array[j] });
j++;
}
}
while (i <= mid) {
temp.push(array[i]);
animations.push({ type: 'overwrite', index: l + temp.length - 1, height: array[i] });
i++;
}
while (j <= r) {
temp.push(array[j]);
animations.push({ type: 'overwrite', index: l + temp.length - 1, height: array[j] });
j++;
}
// Copy temp back to array
for (let k = 0; k < temp.length; k++) {
array[l + k] = temp[k];
animations.push({ type: 'final', index: l + k, height: temp[k] });
}
}
// Play animations one by one
function playAnimations(array) {
animations.forEach((action, i) => {
setTimeout(() => {
if (action.type === 'compare') {
const [a, b] = action.indices;
allBars[a].style.backgroundColor = 'red';
allBars[b].style.backgroundColor = 'red';
setTimeout(() => {
allBars[a].style.backgroundColor = '';
allBars[b].style.backgroundColor = '';
}, 150);
} else if (action.type === 'overwrite') {
allBars[action.index].style.height = action.height + 'px';
allBars[action.index].style.backgroundColor = 'green';
} else if (action.type === 'final') {
allBars[action.index].style.height = action.height + 'px';
allBars[action.index].style.backgroundColor = '#0047AB';
}
}, i * 1000/speed);
});
}
function insertionSort() {
const allBars = document.querySelectorAll(".bar");
const heights = Array.from(allBars).map(bar => parseInt(bar.style.height));
delay = 0;
for (let i = 1; i < heights.length; i++) {
let temp = heights[i];
let j = i - 1;
((i) => {
setTimeout(() => {
allBars[i].style.backgroundColor = 'yellow';
}, delay);
})(i);
delay += 1000 / speed;
while (j >= 0 && heights[j] > temp) {
heights[j + 1] = heights[j];
((j) => {
setTimeout(() => {
allBars[j].style.backgroundColor = "red";
allBars[j + 1].style.height = allBars[j].style.height;
setTimeout(() => {
allBars[j].style.backgroundColor = "#0047AB";
}, 200);
}, delay);
})(j);
j--;
delay += 1000 / speed;
}
heights[j + 1] = temp;
((j, temp) => {
setTimeout(() => {
allBars[j + 1].style.height = temp + "px";
allBars[j + 1].style.backgroundColor = 'green';
setTimeout(() => {
allBars[j + 1].style.backgroundColor = "#0047AB";
}, 400);
}, delay);
})(j, temp);
delay += 1000 / speed;
}
// Final blue pass
for (let i = 0; i < allBars.length; i++) {
setTimeout(() => {
allBars[i].style.backgroundColor = '#0047AB';
}, delay);
delay += 1000 / speed;
}
}