forked from KRISHNA-GOPALA/sorting-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
375 lines (303 loc) · 11 KB
/
script.js
File metadata and controls
375 lines (303 loc) · 11 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
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
let bars = [];
const def = "#fd0081", chng = "#431f91", finished = "#8ef511", selected = "yellow";
window.onload = setup();
async function setup() {
let b = document.getElementById("bars");
let d = document.getElementById("delay");
document.getElementById("b").innerText = b.value;
document.getElementById("d").innerText = d.value + "ms";
if (bars.length != parseInt(b.value)) {
generateBars(parseInt(b.value));
}
}
function reset() {
location.reload();
}
function Disable_The_Input() {
let x = document.getElementsByTagName("input");
for (let i = 0; i < x.length; i++)
x[i].disabled = true;
return parseInt(document.getElementById("delay").value);
}
function Finished_Sorting() {
let x = document.getElementsByClassName("bar");
for (let i = 0; i < x.length; i++)
x[i].style.backgroundColor = finished;
x = document.getElementsByTagName("input");
for (let i = 0; i < x.length; i++)
x[i].disabled = false;
}
function generateBars(n = -1) {
bars = [];
let container = document.getElementById("container");
n = n < 0 ? Math.random() * 20 : n;
for (let i = 0; i < n; i++) {
bars.push('<div class="bar" id="' + i + '" style="height:' + Math.floor(2 + Math.random() * 98) + '%"></div>');
}
container.innerHTML = bars.join('');
}
function Sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function MapRange(value, in_min, in_max, out_min, out_max) {
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//=============================== Sorting Algorithms ==================================//
// 1
// SELECTION SORT
// SelectionSort() : Implementation of selection sort algorithm. O(n^2)
async function SelectionSort() {
let delay = Disable_The_Input();
let container = document.getElementById("container");
for (let i = 0; i < bars.length; i++) {
let mn_ind = i;
let curr_id = bars[i].split('id="')[1].split('"')[0];
document.getElementById(curr_id).style.backgroundColor = selected;
let sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, delay)
for (let j = i + 1; j < bars.length; j++) {
let nxt_ele = bars[j].split('id="')[1].split('"')[0];
document.getElementById(nxt_ele).style.backgroundColor = chng;
let a = parseInt(bars[mn_ind].split(/[:%]/)[1]);
let b = parseInt(bars[j].split(/[:%]/)[1]);
if (a > b) mn_ind = j;
await Sleep(delay / 5.0);
document.getElementById(nxt_ele).style.backgroundColor = def;
}
let nxt_ele = bars[mn_ind].split('id="')[1].split('"')[0];
document.getElementById(nxt_ele).style.backgroundColor = selected;
await Sleep(2 * delay / 5.0);
let tmp = bars[mn_ind];
bars[mn_ind] = bars[i];
bars[i] = tmp;
container.innerHTML = bars.join('');
await Sleep(2 * delay / 5.0);
document.getElementById(curr_id).style.backgroundColor = def;
document.getElementById(nxt_ele).style.backgroundColor = def;
}
Finished_Sorting();
}
// 2
// BUBBLE SORT
// BubbleSort() : Implementation of bubble sort algorithm. O(n^2)
async function BubbleSort() {
let delay = Disable_The_Input();
let container = document.getElementById("container");
for (let i = 0; i < bars.length - 1; i++) {
let has_swap = false;
for (let j = 0; j < bars.length - i - 1; j++) {
let curr_id = bars[j].split('id="')[1].split('"')[0];
let nxt_ele = bars[j + 1].split('id="')[1].split('"')[0];
document.getElementById(curr_id).style.backgroundColor = selected;
let sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, delay)
document.getElementById(nxt_ele).style.backgroundColor = chng;
await Sleep(delay / 2);
let a = parseInt(bars[j].split(/[:%]/)[1]);
let b = parseInt(bars[j + 1].split(/[:%]/)[1]);
if (a > b) {
has_swap = true;
let t = bars[j];
bars[j] = bars[j + 1];
bars[j + 1] = t;
container.innerHTML = bars.join('');
}
document.getElementById(curr_id).style.backgroundColor = selected;
document.getElementById(nxt_ele).style.backgroundColor = chng;
await Sleep(delay / 2.0);
document.getElementById(curr_id).style.backgroundColor = def;
document.getElementById(nxt_ele).style.backgroundColor = def;
}
if (has_swap == false) break;
}
Finished_Sorting();
}
// 3
// INSERTION SORT
// InsertionSort() : Implementation of inserion sort algorithm. O(n^2)
async function InsertionSort() {
let delay = Disable_The_Input();
let container = document.getElementById("container");
for (let i = 1; i < bars.length; i++) {
let j = i - 1;
let key = bars[i];
let curr_id = key.split('id="')[1].split('"')[0];
let nxt_ele = bars[j].split('id="')[1].split('"')[0];
document.getElementById(curr_id).style.backgroundColor = selected;
let sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, delay)
while (j >= 0 && parseInt(bars[j].split(/[:%]/)[1]) > parseInt(key.split(/[:%]/)[1])) {
document.getElementById(nxt_ele).style.backgroundColor = def;
nxt_ele = bars[j].split('id="')[1].split('"')[0];
document.getElementById(nxt_ele).style.backgroundColor = chng;
await Sleep(delay);
bars[j + 1] = bars[j];
j--;
}
bars[j + 1] = key;
container.innerHTML = bars.join('');
document.getElementById(curr_id).style.backgroundColor = selected;
document.getElementById(nxt_ele).style.backgroundColor = chng;
await Sleep(delay * 3.0 / 5);
document.getElementById(curr_id).style.backgroundColor = def;
document.getElementById(nxt_ele).style.backgroundColor = def;
}
Finished_Sorting();
}
// 4
// MERGE SORT
// Slide_down() : Places bars[r] at lth position by sliding other bars to the right.
function Slide_down(l, r) {
let temp = bars[r];
for (let i = r - 1; i >= l; i--) {
bars[i + 1] = bars[i];
}
bars[l] = temp;
}
async function merge(l, m, r, d) {
let y = l;
let i = l;
let j = m + 1;
while (i < j && j <= r) {
let curr_id = bars[j].split('id="')[1].split('"')[0];
let nxt_ele = bars[i].split('id="')[1].split('"')[0];
document.getElementById(curr_id).style.backgroundColor = selected;
document.getElementById(nxt_ele).style.backgroundColor = chng;
let a = parseInt(bars[j].split(/[:%]/)[1]);
let b = parseInt(bars[i].split(/[:%]/)[1]);
if (a > b) i++;
else {
Slide_down(i, j);
i++; j++;
}
await Sleep(d / 2.0);
container.innerHTML = bars.join('');
document.getElementById(curr_id).style.backgroundColor = selected;
document.getElementById(nxt_ele).style.backgroundColor = chng;
let sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, d)
await Sleep(d / 2.0);
document.getElementById(curr_id).style.backgroundColor = def;
document.getElementById(nxt_ele).style.backgroundColor = def;
sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, d)
}
}
async function mergeSort(l, r, d) {
if (l < r) {
let m = parseInt(l + (r - l) / 2);
await mergeSort(l, m, d);
await mergeSort(m + 1, r, d);
await merge(l, m, r, d);
}
}
async function MergeSort() {
let delay = Disable_The_Input();
await mergeSort(0, bars.length - 1, delay);
Finished_Sorting();
}
// 5
// QUICK SORT
// Partition(): Places the (r)th bar at the correct position
async function Partition(l, r, d) {
let i = l - 1;
let j = l;
let id = bars[r].split('id="')[1].split('"')[0];
document.getElementById(id).style.backgroundColor = selected;
for (j = l; j < r; j++) {
let a = parseInt(bars[j].split(/[:%]/)[1]);
let b = parseInt(bars[r].split(/[:%]/)[1]);
if (a < b) {
i++;
let curr_id = bars[i].split('id="')[1].split('"')[0];
let nxt_ele = bars[j].split('id="')[1].split('"')[0];
document.getElementById(curr_id).style.backgroundColor = chng;
document.getElementById(nxt_ele).style.backgroundColor = chng;
let temp = bars[i];
bars[i] = bars[j];
bars[j] = temp;
await Sleep(d / 3.0);
container.innerHTML = bars.join('');
document.getElementById(curr_id).style.backgroundColor = chng;
document.getElementById(nxt_ele).style.backgroundColor = chng;
document.getElementById(id).style.backgroundColor = selected;
let sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, d)
await Sleep(d / 3.0)
document.getElementById(curr_id).style.backgroundColor = def;
document.getElementById(nxt_ele).style.backgroundColor = def;
}
}
let temp = bars[i + 1];
bars[i + 1] = bars[r];
bars[r] = temp;
container.innerHTML = bars.join(' ');
document.getElementById(id).style.backgroundColor = selected;
await Sleep(d / 3.0);
document.getElementById(id).style.backgroundColor = def;
return i + 1;
}
async function quickSort(l, r, d) {
if (l < r) {
let p = await Partition(l, r, d);
await quickSort(l, p - 1, d);
await quickSort(p + 1, r, d);
}
}
async function QuickSort() {
let delay = Disable_The_Input();
await quickSort(0, bars.length - 1, delay);
Finished_Sorting();
}
// 6
// HEAP SORT
// Heapfiy(): Creates a max heap.
async function Heapfiy(n, i, d) {
let largest = i;
let l = 2 * i + 1; // lft
let r = 2 * i + 2; // rgt
let curr_id = bars[i].split('id="')[1].split('"')[0];
let nxt_ele;
let id3;
document.getElementById(curr_id).style.backgroundColor = selected;
if (r < n) {
id3 = bars[r].split('id="')[1].split('"')[0];
document.getElementById(id3).style.backgroundColor = chng;
}
if (l < n) {
nxt_ele = bars[l].split('id="')[1].split('"')[0];
document.getElementById(nxt_ele).style.backgroundColor = chng;
}
await Sleep(d / 3.0)
if (l < n && parseInt(bars[l].split(/[:%]/)[1]) > parseInt(bars[largest].split(/[:%]/)[1]))
largest = l;
if (r < n && parseInt(bars[r].split(/[:%]/)[1]) > parseInt(bars[largest].split(/[:%]/)[1]))
largest = r;
if (largest != i) {
let t = bars[i]; bars[i] = bars[largest]; bars[largest] = t;
container.innerHTML = bars.join(' ');
document.getElementById(curr_id).style.backgroundColor = selected;
let sound = MapRange(document.getElementById(curr_id).style.height.split('%')[0], 2, 100, 500, 1000);
beep(100, sound, d)
if (r < n) document.getElementById(id3).style.backgroundColor = chng;
if (l < n) document.getElementById(nxt_ele).style.backgroundColor = chng;
await Sleep(d / 3.0)
container.innerHTML = bars.join(' ');
await Heapfiy(n, largest, d);
}
container.innerHTML = bars.join(' ');
}
async function HeapSort() {
let delay = Disable_The_Input();
let n = bars.length;
for (let i = n / 2 - 1; i >= 0; i--) // Build the heap
await Heapfiy(n, i, delay);
for (let i = n - 1; i >= 0; i--) {
let t = bars[0]; // Swaping
bars[0] = bars[i];
bars[i] = t;
container.innerHTML = bars.join(' ');
await Heapfiy(i, 0, delay);
}
Finished_Sorting();
}