This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.js
456 lines (378 loc) · 16.6 KB
/
visualize.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
function sleep(ms) {
return new Promise(resolve => window.setTimeout(resolve, ms));
}
function refresh(movm, comp) {
document.getElementById('comparisons').innerHTML = comp;
document.getElementById('movements').innerHTML = movm;
document.getElementById('runtime').innerHTML = ((new Date()).getTime() - glob_stime) / 1000.0;
}
function changeTheme(theme) {
var xhtp = new XMLHttpRequest();
xhtp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementsByTagName('style')[0].innerHTML = this.responseText;
}
}
var avThemes = ["dark", "light"];
if (!avThemes.includes(theme))
return -1;
glob_theme = theme;
if (document.getElementById('theme_in')!==null)
document.getElementById('theme_in').value = theme;
// window.history.pushState(`${theme.toUpperCase()}-THEME`, 'Changed theme ... ', `${theme}`);
xhtp.open("GET", "themes/" + theme + ".css");
xhtp.send();
}
function display_array_pillars(arr, focused_els_i) {
focused_els_i = focused_els_i || [];
ctx.clearRect(0, 0, canvas.width, canvas.height);
let scale = canvas.height / Math.max(...arr);
for (let i = canvas.width / (arr.length + 1); i <= canvas.width; i += canvas.width / (arr.length + 1)) {
ctx.beginPath();
ctx.strokeStyle = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? "red" : glob_themes[glob_theme]["color"];
ctx.lineWidth = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 4 : 2;
ctx.moveTo(i, canvas.height);
ctx.lineTo(i, canvas.height - (arr[Math.floor(i / (canvas.width / (arr.length + 1))) - 1] * scale));
ctx.stroke();
}
}
function display_array_color_pillars(arr) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let scale = canvas.height / Math.max(...arr);
let line_weight = 2,
arr_max = Math.max(...arr);
for (let i = canvas.width / (arr.length - 1); i <= canvas.width; i += canvas.width / (arr.length - 1)) {
ctx.beginPath();
ctx.strokeStyle = "hsl(" + arr[Math.floor(i / (canvas.width / (arr.length - 1)))] * (360 / arr_max) + ", 100%, 50%)";
ctx.lineWidth = line_weight;
ctx.moveTo(i, canvas.height);
ctx.lineTo(i, canvas.height - (arr[Math.floor(i / (canvas.width / (arr.length - 1))) - 1] * scale));
ctx.stroke();
}
}
function display_array_pyramid (arr, focused_els_i) {
focused_els_i = focused_els_i || [];
ctx.clearRect(0, 0, canvas.width, canvas.height);
let scale = canvas.width / Math.max(...arr);
for (let i = canvas.height / (arr.length + 1); i <= canvas.height; i += canvas.height / (arr.length + 1)) {
ctx.beginPath();
ctx.strokeStyle = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? "red" : glob_themes[glob_theme]["color"];
ctx.lineWidth = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 4 : 2;
ctx.moveTo(canvas.width/2-(arr[Math.floor(i / (canvas.width / (arr.length + 1))) - 1] * scale)/2, i);
ctx.lineTo(canvas.width/2+(arr[Math.floor(i / (canvas.width / (arr.length + 1))) - 1] * scale)/2, i);
ctx.stroke();
}
}
function display_array_color_pyramid (arr) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let scale = canvas.height / Math.max(...arr);
let line_weight = 2,
arr_max = Math.max(...arr);
for (let i = canvas.width / (arr.length - 1); i <= canvas.width; i += canvas.width / (arr.length - 1)) {
ctx.beginPath();
ctx.strokeStyle = "hsl(" + arr[Math.floor(i / (canvas.width / (arr.length - 1)))] * (360 / arr_max) + ", 100%, 50%)";
ctx.lineWidth = line_weight;
ctx.moveTo(canvas.width/2-(arr[Math.floor(i / (canvas.width / (arr.length + 1))) - 1] * scale)/2, i);
ctx.lineTo(canvas.width/2+(arr[Math.floor(i / (canvas.width / (arr.length + 1))) - 1] * scale)/2, i);
ctx.stroke();
}
}
function display_array_pillar_spiral(arr, focused_els_i) {
focused_els_i = focused_els_i || [];
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 360 / (arr.length - 1), c = 0; i < 360; i += 360 / (arr.length - 1), c++) {
ctx.beginPath();
ctx.strokeStyle = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? "red" : glob_themes[glob_theme]["color"];
ctx.lineWidth = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 4 : 2;
ctx.moveTo(canvas.width / 2, canvas.height / 2);
ctx.lineTo((canvas.width / 2) + arr[c] * Math.cos(Math.PI * i / 180.0), (canvas.height / 2) + arr[c] * Math.sin(Math.PI * i / 180.0));
ctx.stroke();
}
}
function display_array_color_pillar_spiral(arr) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let line_weight = 2,
arr_max = Math.max(...arr);
for (let i = 360 / (arr.length - 1), c = 0; i < 360; i += 360 / (arr.length - 1), c++) {
ctx.beginPath();
ctx.strokeStyle = "hsl(" + arr[c] * (360 / arr_max) + ", 100%, 50%)";
ctx.lineWidth = line_weight;
ctx.moveTo(canvas.width / 2, canvas.height / 2);
ctx.lineTo((canvas.width / 2) + arr[c] * Math.cos(Math.PI * i / 180.0), (canvas.height / 2) + arr[c] * Math.sin(Math.PI * i / 180.0));
ctx.stroke();
}
}
function display_array_color_circle(arr) {
let arr_max = Math.max(...arr),
line_weight = 3;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let r = canvas.width / 3;
for (let i = 360 / (arr.length - 1), c = 0; i < 360; i += 360 / (arr.length - 1), c++) {
ctx.beginPath();
ctx.strokeStyle = "hsl(" + arr[c] * (360 / arr_max) + ", 100%, 50%)";
ctx.lineWidth = line_weight;
ctx.moveTo(canvas.width / 2, canvas.height / 2);
ctx.lineTo((canvas.width / 2) + r * Math.cos(Math.PI * i / 180.0), (canvas.height / 2) + r * Math.sin(Math.PI * i / 180.0));
ctx.stroke();
}
ctx.strokeStyle = "black";
ctx.lineWidth = 1;
}
function display_array_dots(arr, focused_els_i) {
focused_els_i = focused_els_i || [];
let radius = 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let scale = canvas.height / Math.max(...arr);
for (let i = canvas.width / (arr.length - 1); i < canvas.width; i += canvas.width / (arr.length - 1)) {
ctx.beginPath();
ctx.fillStyle = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? "red" : glob_themes[glob_theme]["color"];
ctx.ellipse(i, canvas.height - (arr[Math.floor(i / (canvas.width / (arr.length - 1)))] * scale),
focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 2 * radius : radius,
focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 2 * radius : radius, 0, 0, 2 * Math.PI);
ctx.fill();
}
}
function display_array_color_dots(arr) {
let radius = 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let scale = canvas.height / Math.max(...arr),
arr_max = Math.max(...arr);
for (let i = canvas.width / (arr.length - 1); i < canvas.width; i += canvas.width / (arr.length - 1)) {
ctx.beginPath();
ctx.fillStyle = "hsl(" + arr[Math.floor(i / (canvas.width / (arr.length - 1)))] * (360 / arr_max) + ", 100%, 50%)";
ctx.ellipse(i, canvas.height - (arr[Math.floor(i / (canvas.width / (arr.length - 1)))] * scale), radius, radius, 0, 0, 2 * Math.PI);
ctx.fill();
}
}
function display_array_dots_spiral(arr, focused_els_i) {
focused_els_i = focused_els_i || [];
ctx.clearRect(0, 0, canvas.width, canvas.height);
let radius = 2;
for (let i = 360 / (arr.length - 1), c = 0; i < 360; i += 360 / (arr.length - 1), c++) {
ctx.beginPath();
ctx.fillStyle = focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? "red" : glob_themes[glob_theme]["color"];
ctx.ellipse((canvas.width / 2) + arr[c] * Math.cos(Math.PI * i / 180.0), (canvas.height / 2) + arr[c] * Math.sin(Math.PI * i / 180.0),
focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 2 * radius : radius,
focused_els_i.includes(Math.floor(i / (canvas.width / (arr.length + 1)))) ? 2 * radius : radius, 0, 0, 2 * Math.PI);
ctx.fill();
}
}
function display_array_color_dots_spiral(arr) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let radius = 2,
arr_max = Math.max(...arr);
for (let i = 360 / (arr.length - 1), c = 0; i < 360; i += 360 / (arr.length - 1), c++) {
ctx.beginPath();
ctx.fillStyle = ctx.fillStyle = "hsl(" + arr[Math.floor(i / (canvas.width / (arr.length - 1)))] * (360 / arr_max) + ", 100%, 50%)";
ctx.ellipse((canvas.width / 2) + arr[c] * Math.cos(Math.PI * i / 180.0), (canvas.height / 2) + arr[c] * Math.sin(Math.PI * i / 180.0), radius,
radius, 0, 0, 2 * Math.PI);
ctx.fill();
}
}
function display_array_color_circle_dots(arr) {
let arr_max = Math.max(...arr),
radius = 3;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let r = canvas.width / 3;
for (let i = 360 / (arr.length - 1), c = 0; i < 360; i += 360 / (arr.length - 1), c++) {
ctx.beginPath();
ctx.fillStyle = "hsl(" + arr[c] * (360 / arr_max) + ", 100%, 50%)";
ctx.ellipse((canvas.width / 2) + r * Math.cos(Math.PI * i / 180.0), (canvas.height / 2) + r * Math.sin(Math.PI * i / 180.0), radius,
radius, 0, 0, 2 * Math.PI);
ctx.fill();
}
ctx.fillStyle = "black";
ctx.lineWidth = 1;
}
function draw_random(amount, upper, lower, display) {
arr = [];
lower = lower || glob_lower;
upper = upper || glob_upper;
for (let i = 0; i < amount; i++)
arr.push(lower + Math.floor(Math.random() * (upper - lower)));
display(arr);
return arr;
}
function draw_semi_random(amount, upper, lower, display) {
arr = [...Array(upper).keys()].splice(lower);
for (let i = 0; i < arr.length; i++) {
let temp = arr[i],
rindex = Math.floor(Math.random() * arr.length);
arr[i] = arr[rindex];
arr[rindex] = temp;
}
display(arr);
return arr;
}
// ------------------------------ GLOBALS ------------------------------------------------------------- //
var glob_amount = 256,
glob_lower = 0,
glob_upper = 150,
glob_sleep_time = 1,
glob_sleep_between = 1000,
glob_display_func = display_array_color_circle,
glob_random_func = draw_semi_random,
glob_stime = (new Date()).getTime(),
glob_theme = "light",
glob_themes = {
"light": {
"color": "dimgray"
},
"dark": {
"color": "white"
}
};
var glob_comp = 0,
glob_movm = 0;
// ---------------------------------------------------------------------------------------------------- //
async function doSortingAlgo(f) {
document.getElementById('algorithm_settings').style.display = "none";
glob_comp = 0;
glob_movm = 0;
refresh(0, 0);
rarr = glob_random_func(glob_amount, glob_upper, glob_lower, glob_display_func);
await f(rarr);
glob_display_func(rarr);
glob_comp = 0;
glob_movm = 0;
document.getElementById('algorithm_settings').style.display = "block";
}
async function doCountingSort(rarr) {
// -- COUNTING SORT -- //
document.getElementById('algorithm_div').innerHTML = "CountingSort";
await cSort(rarr, glob_display_func);
}
async function doQuickSort(rarr) {
// -- QUICKSORT -- //
document.getElementById('algorithm_div').innerHTML = "QuickSort";
await qSort(rarr, 0, rarr.length - 1, glob_display_func)
}
async function doInsertionSort(rarr) {
// -- INSERTION SORT -- //
document.getElementById('algorithm_div').innerHTML = "InsertionSort";
await iSort(rarr, glob_display_func);
}
async function doSelectionSort(rarr) {
// -- SELECTION SORT -- //
document.getElementById('algorithm_div').innerHTML = "SelectionSort";
await sSort(rarr, glob_display_func);
}
async function doBubbleSort(rarr) {
// -- SELECTION SORT -- //
document.getElementById('algorithm_div').innerHTML = "BubbleSort";
await bSort(rarr, glob_display_func);
}
async function doHeapSort(rarr) {
// -- HEAPSORT -- //
document.getElementById('algorithm_div').innerHTML = "HeapSort";
await hSort(rarr, glob_display_func);
}
async function doRadixSort_i(rarr) {
// -- RADIX SORT -- //
document.getElementById('algorithm_div').innerHTML = "RadixSort [with InsertionSort]";
await rSort_i(rarr, glob_display_func);
}
async function doRadixSort_c(rarr) {
// -- RADIX SORT -- //
document.getElementById('algorithm_div').innerHTML = "RadixSort [with CountingSort]";
await rSort_c(rarr, glob_display_func);
}
async function doBitonicSort(rarr) {
// -- BITONIC SORT -- //
document.getElementById('algorithm_div').innerHTML = "BitonicSort";
await bitSort(rarr, glob_display_func);
}
async function doShellSort(rarr) {
// -- SHELL SORT -- //
document.getElementById('algorithm_div').innerHTML = "ShellSort";
await shellSort(rarr, glob_display_func);
}
async function doMergeSort(rarr) {
// -- MERGE SORT -- //
document.getElementById('algorithm_div').innerHTML = "MergeSort";
await mergeSort(rarr, 0, rarr.length - 1, glob_display_func);
}
async function doBogoSort(rarr) {
// -- BOGO SORT -- //
document.getElementById('algorithm_div').innerHTML = "BogoSort";
await bogoSort(rarr, glob_display_func);
}
async function doCocktailSort(rarr) {
// -- COCKTAIL SORT -- //
document.getElementById('algorithm_div').innerHTML = "CocktailShaker-Sort";
await cocktailSort(rarr, glob_display_func);
}
async function doGnomeSort(rarr) {
// -- GNOME SORT -- //
document.getElementById('algorithm_div').innerHTML = "GnomeSort";
await gnomeSort(rarr, glob_display_func);
}
async function doCombSort(rarr) {
// -- COMB SORT -- //
document.getElementById('algorithm_div').innerHTML = "CombSort";
await combSort(rarr, glob_display_func);
}
async function doTreeSort(rarr) {
// -- TREE SORT -- //
document.getElementById('algorithm_div').innerHTML = "TreeSort";
await treeSort(rarr, glob_display_func);
}
async function doIntroSort(rarr) {
// -- INTRO SORT -- //
document.getElementById('algorithm_div').innerHTML = "IntroSort";
await introSort(rarr, glob_display_func);
}
async function doSlowSort(rarr) {
// -- INTRO SORT -- //
document.getElementById('algorithm_div').innerHTML = "SlowSort";
await slowSort(rarr, 0, rarr.length - 1, glob_display_func);
}
async function doStoogeSort(rarr) {
// -- INTRO SORT -- //
document.getElementById('algorithm_div').innerHTML = "StoogeSort";
await stoogeSort(rarr, 0, rarr.length - 1, glob_display_func);
}
async function doDSelectionSort(rarr) {
// -- DOUBLE SELECTION SORT -- //
document.getElementById('algorithm_div').innerHTML = "Double SelectionSort";
await dSelectionSort(rarr, glob_display_func);
}
async function doGravitySort(rarr) {
// -- GRAVITY SORT -- //
document.getElementById('algorithm_div').innerHTML = "GravitySort";
await gravitySort(rarr, glob_display_func);
}
async function doCocktailMergeSort(rarr) {
// -- COCKTAIL-MERGE SORT -- //
document.getElementById('algorithm_div').innerHTML = "Cocktail-MergeSort";
await cmSort(rarr, 0, rarr.length - 1, Math.floor(rarr.length / 8), glob_display_func);
}
async function doQuickMergeSort(rarr) {
// -- QUICK-MERGE SORT -- //
document.getElementById('algorithm_div').innerHTML = "Quick-MergeSort";
await qmSort(rarr, 0, rarr.length - 1, Math.floor(rarr.length / 8), glob_display_func);
}
async function doOddEvenSort(rarr) {
// -- ODD EVEN SORT -- //
document.getElementById('algorithm_div').innerHTML = "OddEvenSort";
await oeSort(rarr, glob_display_func);
}
async function doOddEvenMergeSort(rarr) {
// -- ODD-EVEN MERGE SORT -- //
document.getElementById('algorithm_div').innerHTML = "OddEven-MergeSort";
await oemSort(rarr, 0, rarr.length - 1, Math.floor(rarr.length / 8), glob_display_func);
}
async function doWeaveSort(rarr) {
// -- ODD-EVEN MERGE SORT -- //
document.getElementById('algorithm_div').innerHTML = "WeaveSort [Merge&Insertion]";
await weaveSort(rarr, 0, rarr.length - 1, Math.floor(rarr.length / 8), glob_display_func);
}
async function visualize_init(amount, upper, lower) {
amount = amount || glob_amount;
upper = upper || glob_upper;
lower = lower || glob_lower;
glob_stime = (new Date()).getTime();
await sleep(glob_sleep_between);
// -- QUICK SORT -- //
doSortingAlgo(doQuickSort);
}