-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
383 lines (341 loc) · 10.7 KB
/
index.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
let randomize_array = document.getElementById("randomize_array_btn");
let sort_btn = document.getElementById("sort_btn");
let bars_container = document.getElementById("bars_container");
let select_algo = document.getElementById("algo");
let speed = document.getElementById("speed");
let slider = document.getElementById("slider");
let minRange = 1;
let maxRange = slider.value;
let numOfBars = slider.value;
let heightFactor = 4;
let speedFactor = 100;
let unsorted_array = new Array(numOfBars);
slider.addEventListener("input", function () {
numOfBars = slider.value;
maxRange = slider.value;
//console.log(numOfBars);
bars_container.innerHTML = "";
unsorted_array = createRandomArray();
renderBars(unsorted_array);
});
speed.addEventListener("change", (e) => {
speedFactor = parseInt(e.target.value);
});
let algotouse = "";
select_algo.addEventListener("change", function () {
algotouse = select_algo.value;
});
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createRandomArray() {
let array = new Array(numOfBars);
for (let i = 0; i < numOfBars; i++) {
array[i] = randomNum(minRange, maxRange);
}
return array;
}
document.addEventListener("DOMContentLoaded", function () {
unsorted_array = createRandomArray();
renderBars(unsorted_array);
});
function renderBars(array) {
for (let i = 0; i < numOfBars; i++) {
let bar = document.createElement("div");
bar.classList.add("bar");
bar.style.height = array[i] * heightFactor + "px";
bars_container.appendChild(bar);
}
}
randomize_array.addEventListener("click", function () {
unsorted_array = createRandomArray();
bars_container.innerHTML = "";
renderBars(unsorted_array);
});
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function bubbleSort(array) {
let bars = document.getElementsByClassName("bar");
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
for (let k = 0; k < bars.length; k++) {
if (k !== j && k !== j + 1) {
bars[k].style.backgroundColor = "aqua";
}
}
let temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
bars[j].style.height = array[j] * heightFactor + "px";
bars[j].style.backgroundColor = "lightgreen";
//bars[j].innerText = array[j];
bars[j + 1].style.height = array[j + 1] * heightFactor + "px";
bars[j + 1].style.backgroundColor = "lightgreen";
//bars[j + 1].innerText = array[j + 1];
await sleep(speedFactor);
}
}
await sleep(speedFactor);
}
return array;
}
async function swap(items, leftIndex, rightIndex, bars) {
var temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
bars[leftIndex].style.height = items[leftIndex] * heightFactor + "px";
bars[leftIndex].style.backgroundColor = "lightgreen";
//bars[leftIndex].innerText = items[leftIndex];
bars[rightIndex].style.height = items[rightIndex] * heightFactor + "px";
bars[rightIndex].style.backgroundColor = "lightgreen";
//bars[rightIndex].innerText = items[rightIndex];
await sleep(speedFactor);
}
async function partition(items, left, right) {
let bars = document.getElementsByClassName("bar");
let pivotIndex = Math.floor((right + left) / 2);
var pivot = items[pivotIndex]; //middle element
bars[pivotIndex].style.backgroundColor = "red";
for (let i = 0; i < bars.length; i++) {
if (i != pivotIndex) {
bars[i].style.backgroundColor = "aqua";
}
}
(i = left), //left pointer
(j = right); //right pointer
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
await swap(items, i, j, bars); //sawpping two elements
i++;
j--;
}
}
return i;
}
async function quickSort(items, left, right) {
var index;
let bars = document.getElementsByClassName("bar");
if (items.length > 1) {
index = await partition(items, left, right); //index returned from partition
if (left < index - 1) {
//more elements on the left side of the pivot
await quickSort(items, left, index - 1);
}
if (index < right) {
//more elements on the right side of the pivot
await quickSort(items, index, right);
}
}
for (let i = 0; i < bars.length; i++) {
bars[i].style.backgroundColor = "aqua";
}
return items;
}
// sort_btn.addEventListener("click", function () {
// let sorted_array = quickSort(unsorted_array, 0, numOfBars - 1);
// console.log(sorted_array);
// });
//write insertion sort function
async function InsertionSort(array) {
let bars = document.getElementsByClassName("bar");
for (let i = 1; i < array.length; i++) {
let key = array[i];
let j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
bars[j + 1].style.height = array[j + 1] * heightFactor + "px";
bars[j + 1].style.backgroundColor = "red";
//bars[j + 1].innerText = array[j + 1];
await sleep(speedFactor);
for (let k = 0; k < bars.length; k++) {
if (k != j + 1) {
bars[k].style.backgroundColor = "aqua";
}
}
j = j - 1;
}
array[j + 1] = key;
bars[j + 1].style.height = array[j + 1] * heightFactor + "px";
bars[j + 1].style.backgroundColor = "lightgreen";
//bars[j + 1].innerText = array[j + 1];
await sleep(speedFactor);
}
for (let k = 0; k < bars.length; k++) {
bars[k].style.backgroundColor = "aqua";
}
return array;
}
//write heap sort function
async function HeapSort(array) {
let bars = document.getElementsByClassName("bar");
for (let i = Math.floor(array.length / 2); i >= 0; i--) {
await heapify(array, array.length, i);
}
for (let i = array.length - 1; i >= 0; i--) {
await swap(array, 0, i, bars);
await heapify(array, i, 0);
}
for (let k = 0; k < bars.length; k++) {
bars[k].style.backgroundColor = "aqua";
await sleep(speedFactor);
}
return array;
}
async function heapify(array, n, i) {
let bars = document.getElementsByClassName("bar");
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && array[left] > array[largest]) {
largest = left;
}
if (right < n && array[right] > array[largest]) {
largest = right;
}
if (largest != i) {
await swap(array, i, largest, bars);
await heapify(array, n, largest);
}
}
async function swap(array, i, j, bars) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
bars[i].style.height = array[i] * heightFactor + "px";
bars[j].style.height = array[j] * heightFactor + "px";
bars[i].style.backgroundColor = "red";
bars[j].style.backgroundColor = "red";
await sleep(speedFactor);
for (let k = 0; k < bars.length; k++) {
if (k != i && k != j) {
bars[k].style.backgroundColor = "aqua";
}
}
//bars[i].innerText = array[i];
//bars[j].innerText = array[j];
return array;
}
//write mergeSort function
async function mergeSort(arr) {
let bars = document.getElementsByClassName("bar");
if (arr.length < 2) {
return arr;
}
const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);
let actualHalf = await mergeSort(left);
await mergeSort(right);
let i = 0;
let j = 0;
let k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
arr[k] = left[i];
i++;
// bars[k].style.height = arr[k] * heightFactor + "px";
// bars[k].style.backgroundColor = "lightgreen";
// bars[k].innerText = arr[k];
//await sleep(speedFactor);
} else {
arr[k] = right[j];
j++;
// bars[k + middle].style.height = arr[k] * heightFactor + "px";
// bars[k + middle].style.backgroundColor = "yellow";
// bars[k].innerText = arr[k];
//await sleep(speedFactor);
}
//shift to right side
//console.log(k);
//bars[k].style.height = arr[k] * heightFactor + "px";
//bars[k].style.backgroundColor = "lightgreen";
// bars[k + middle].style.height = arr[k] * heightFactor + "px";
// bars[k + middle].style.backgroundColor = "yellow";
//visualize it for right and left side
bars[k].style.height = arr[k] * heightFactor + "px";
bars[k].style.backgroundColor = "lightgreen";
if (k + arr.length < bars.length) {
bars[k + arr.length].style.height = arr[k] * heightFactor + "px";
console.log(arr[k] * heightFactor);
bars[k + arr.length].style.backgroundColor = "yellow";
}
await sleep(speedFactor);
//bars[k].innerText = arr[k];
k++;
}
while (i < left.length) {
arr[k] = left[i];
bars[k].style.height = arr[k] * heightFactor + "px";
bars[k].style.backgroundColor = "lightgreen";
await sleep(speedFactor);
i++;
k++;
}
while (j < right.length) {
arr[k] = right[j];
bars[k].style.height = arr[k] * heightFactor + "px";
bars[k].style.backgroundColor = "lightgreen";
await sleep(speedFactor);
j++;
k++;
}
// for (let i = 0; i < arr.length; i++) {
// bars[i].style.height = arr[i] * heightFactor + "px";
// bars[i].style.backgroundColor = "lightgreen";
// await sleep(speedFactor);
// }
for (let k = 0; k < bars.length; k++) {
bars[k].style.backgroundColor = "aqua";
}
return arr;
}
function mergeSortD(arr, start, end) {
if (arr.length < 2) {
return arr;
}
let middle = Math.floor((start + end) / 2);
let left = arr.slice(start, middle);
let right = arr.slice(middle, end);
//mergeSort(left);
mergeSort(right);
}
sort_btn.addEventListener("click", function () {
switch (algotouse) {
case "bubble":
bubbleSort(unsorted_array);
break;
case "merge":
if (
confirm(
"Merge Sort is not visualized properly. Do you want to continue?"
)
) {
mergeSort(unsorted_array);
} else {
break;
}
//console.log(mergeSort(unsorted_array));
break;
case "heap":
HeapSort(unsorted_array);
break;
case "insertion":
InsertionSort(unsorted_array);
break;
case "quick":
console.log(unsorted_array.length);
quickSort(unsorted_array, 0, unsorted_array.length - 1);
break;
default:
bubbleSort(unsorted_array);
break;
}
});