-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
712 lines (605 loc) · 25.3 KB
/
index.html
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Algorithm visualizer | Supratik Basu</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
padding: 32px 64px;
color: #2f363f;
background-color: rgba(249,250,252, 0.5);;
}
.bar {
width: 5px;
display: inline-block;
margin: 0 1px;
background-color: blue;
transition: height 100ms ease-in;
}
h1, h3 {
font-family: 'Courier New', Courier, monospace;
}
h1 {
margin-bottom: 1.5rem;
}
h3 {
margin-bottom: 3rem;
}
.btn {
background-color:#1f7ae0;
border: 1px solid#1f7ae0;
color:white;
display: inline-block;
white-space: nowrap;
font-family: "Graphik Web Medium", sans-serif;
font-size: 13px;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
border: solid 1px transparent;
cursor: pointer;
padding: 9px 13px;
margin: 0 12px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
outline: none;
-webkit-transition: -webkit-transform 0.15s ease, -webkit-box-shadow 0.15s ease;
transition: -webkit-transform 0.15s ease, -webkit-box-shadow 0.15s ease;
-o-transition: box-shadow 0.15s ease, -o-transform 0.15s ease;
-moz-transition: transform 0.15s ease, box-shadow 0.15s ease, -moz-transform 0.15s ease, -moz-box-shadow 0.15s ease;
transition: transform 0.15s ease, box-shadow 0.15s ease;
transition: transform 0.15s ease, box-shadow 0.15s ease, -webkit-transform 0.15s ease, -moz-transform 0.15s ease, -o-transform 0.15s ease, -webkit-box-shadow 0.15s ease, -moz-box-shadow 0.15s ease;
}
.btn:hover {
-webkit-transform: translateY(-1px);
-moz-transform: translateY(-1px);
-ms-transform: translateY(-1px);
-o-transform: translateY(-1px);
transform: translateY(-1px);
-webkit-box-shadow: 0 3px 10px 0 rgba(0, 0, 0, 0.07);
-moz-box-shadow: 0 3px 10px 0 rgba(0, 0, 0, 0.07);
box-shadow: 0 3px 10px 0 rgba(0, 0, 0, 0.07);
}
.btn:disabled {
background-color:#1f7ae077;
}
.ipt {
padding: 11px 16px;
line-height: 1.3;
width: 220px;
font-size: 14px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
padding: 12px;
border: solid 1px rgba(83, 91, 98, 0.25);
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
outline: none;
color:#2f363f;
margin: 0 12px;
}
.ipt:first-child {
margin: 0;
}
.controls {
display: flex;
justify-content: flex-start;
}
#lines-container {
margin-top: 64px;
min-height: 600px;
padding: 32px;
}
/* CSS of toast reference: https://codepen.io/kipp0/pen/pPNrrj */
#toast {
visibility: hidden;
max-width: 50px;
height: 50px;
/*margin-left: -125px;*/
margin: auto;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 2px;
position: fixed;
z-index: 1;
left: 0;right:0;
bottom: 30px;
font-size: 16px;
white-space: nowrap;
}
#toast #toast-img{
width: 50px;
height: 50px;
float: left;
padding-top: 16px;
padding-bottom: 16px;
box-sizing: border-box;
background-color: #111;
color: #fff;
}
#toast-img{
display: flex;
justify-content: center;
align-items: center;
}
#toast #toast-desc{
color: #fff;
padding: 16px;
overflow: hidden;
white-space: nowrap;
}
#toast.show {
visibility: visible;
-webkit-animation: fadein 0.5s, expand 0.5s 0.5s,stay 3s 1s, shrink 0.5s 2s, fadeout 0.5s 2.5s;
animation: fadein 0.5s, expand 0.5s 0.5s,stay 3s 1s, shrink 0.5s 4s, fadeout 0.5s 4.5s;
}
@-webkit-keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@keyframes fadein {
from {bottom: 0; opacity: 0;}
to {bottom: 30px; opacity: 1;}
}
@-webkit-keyframes expand {
from {min-width: 50px}
to {min-width: 350px}
}
@keyframes expand {
from {min-width: 50px}
to {min-width: 350px}
}
@-webkit-keyframes stay {
from {min-width: 350px}
to {min-width: 350px}
}
@keyframes stay {
from {min-width: 350px}
to {min-width: 350px}
}
@-webkit-keyframes shrink {
from {min-width: 350px;}
to {min-width: 50px;}
}
@keyframes shrink {
from {min-width: 350px;}
to {min-width: 50px;}
}
@-webkit-keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 60px; opacity: 0;}
}
@keyframes fadeout {
from {bottom: 30px; opacity: 1;}
to {bottom: 60px; opacity: 0;}
}
/* ***************************** */
</style>
</head>
<body>
<h1>Algorithm visualizer</h1>
<h3>Linear Search, Binary Search, Bubble Sort, Selection Sort</h3>
<div class="controls">
<input type="number" id="dataset-size" class="ipt" placeholder="Dataset size (min 5)" onblur="handleDatasetSizeChange()"/>
<input type="number" id="search-number" class="ipt" placeholder="Enter number for search" onblur="handleSearchNumberChange()"/>
<button id="generate-array" class="btn" onclick="generateArray()">Generate Array</button>
<button id="linear-search" class="btn" onclick="doLinearSearch()">Linear Search</button>
<button id="binary-search" class="btn" onclick="doBinarySearch()">Binary Search</button>
<button id="bubble-sort" class="btn" onclick="doBubbleSort()">Bubble Sort</button>
<button id="selection-sort" class="btn" onclick="doSelectionSort()">Selection Sort</button>
</div>
<div id="lines-container">
</div>
<div id="toast">
<div id="toast-img"><i id="toast-icon" class="fa fa-2x fa-check-circle" aria-hidden="true" style="color: #d0f0c0;"></i></div>
<div id="toast-desc"></div>
</div>
<script>
// constants
const BG_COLOR = 'blue'
const COMPARE_COLOR = 'yellow'
const FOUND_COLOR = 'green'
const SELECT_COLOR = 'red'
// utils
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
</script>
<script>
// Algorithms for animations
function getLinearSearchAnimations(searchNumber, array) {
let animationArr = []
for (let i = 0; i < array.length; ++i) {
if (searchNumber === array[i]) {
animationArr.push({
pos: i,
found: true,
})
return animationArr
} else {
animationArr.push({
pos: i,
})
}
}
animationArr.push({
pos: array.length - 1,
found: false
})
return animationArr
}
function getBinarySearchAnimations(searchNumber, sortedArray) {
let animationArr = []
let lowIndex = 0
let highIndex = sortedArray.length - 1
let midIndex
animationArr.push({
lowIndex,
highIndex,
status: 'compare',
})
while (lowIndex <= highIndex) {
midIndex = Math.floor((lowIndex + highIndex) / 2)
animationArr.push({
midIndex,
status: 'select'
})
if (sortedArray[midIndex] == searchNumber) {
animationArr.push({
midIndex,
status: 'found'
})
return animationArr
} else if (sortedArray[midIndex] < searchNumber) {
lowIndex = midIndex + 1
animationArr.push({
lowIndex,
highIndex,
status: 'compare',
})
} else {
highIndex = midIndex - 1
animationArr.push({
lowIndex,
highIndex,
status: 'compare',
})
}
}
animationArr.push({
lowIndex,
midIndex,
highIndex,
status: 'not-found',
})
return animationArr;
}
function getBubbleSortAnimations(inputArr) {
let animationArr = []
let len = inputArr.length;
let swapped;
do {
swapped = false;
for (let i = 0; i < len - 1; i++) {
animationArr.push({
posI: i,
posJ: i + 1,
status: 'compare',
})
if (inputArr[i] > inputArr[i + 1]) {
animationArr.push({
posI: i,
posJ: i + 1,
status: 'swap',
})
let tmp = inputArr[i];
inputArr[i] = inputArr[i + 1];
inputArr[i + 1] = tmp;
swapped = true;
}
}
} while (swapped);
return animationArr;
}
function getSelectionSortAnimations(inputArr) {
let animationArr = []
let len = inputArr.length;
for (let i = 0; i < len; i++) {
let min = i
animationArr.push({
min: i,
status: 'select-min',
})
for (let j = i + 1; j < len; j++) {
animationArr.push({
min,
j,
status: 'compare'
})
if (inputArr[min] > inputArr[j]) {
animationArr.push({
min: j,
status: 'select-min',
})
min = j
}
}
if (min !== i) {
animationArr.push({
min,
i,
status: 'swap'
})
let tmp = inputArr[i]
inputArr[i] = inputArr[min]
inputArr[min] = tmp
}
}
return animationArr
}
</script>
<script>
// Project utitlity functions
function setInactiveInteractions(active) {
document.getElementById('dataset-size').disabled = active
document.getElementById('search-number').disabled = active
document.getElementById('generate-array').disabled = active
document.getElementById('linear-search').disabled = active
document.getElementById('binary-search').disabled = active
document.getElementById('bubble-sort').disabled = active
document.getElementById('selection-sort').disabled = active
}
function enableInteractions() {
setInactiveInteractions(false)
}
function disableInteractions() {
setInactiveInteractions(true)
}
function toast(type, desc) {
let toast = document.getElementById("toast")
let toastDesc = document.getElementById("toast-desc")
let toastIcon = document.getElementById("toast-icon")
toast.className = "show";
toastDesc.innerText = desc
toastIcon.className = type === 'success' ? 'fa fa-2x fa-check-circle' : 'fa fa-2x fa-exclamation-circle'
toastIcon.style.color = type === 'success' ? '#d0f0c0' : '#e32636'
setTimeout(() => { toast.className = toast.className.replace("show", ""); }, 5000);
}
</script>
<script>
let datasetSize, searchNumber, array = [];
function handleDatasetSizeChange() {
datasetSize = Number(document.getElementById('dataset-size').value)
if (!datasetSize || datasetSize < 5) {
toast('fail', 'Dataset size must be minimum 5')
document.getElementById('dataset-size').focus()
datasetSize = null
return
}
}
function handleSearchNumberChange() {
searchNumber = Number(document.getElementById('search-number').value) >= 5 ? Number(document.getElementById('search-number').value) : 5
document.getElementById('search-number').value = searchNumber
}
function generateArray() {
if (!datasetSize || datasetSize < 5) {
toast('fail', 'Input dataset size must be minimum 5')
document.getElementById('dataset-size').focus()
return
}
let lineContainerElement = document.getElementById('lines-container')
// Remove Present Children
let child = lineContainerElement.lastElementChild;
while (child) {
lineContainerElement.removeChild(child);
child = lineContainerElement.lastElementChild;
}
array = []
// Add new Children
for (let i = 0; i < datasetSize; ++i) {
let randomNumber = getRandomNumber(5, 500)
array.push(randomNumber)
let lineElement = document.createElement('div')
lineElement.className = "bar"
lineElement.style.height = randomNumber + 'px'
lineContainerElement.appendChild(lineElement)
}
}
async function doLinearSearch() {
if (!searchNumber) {
toast('fail', 'Enter search number >= 5!')
document.getElementById('search-number').focus()
return
}
if (!array.length) {
toast('fail', 'Generate Array')
document.getElementById('generate-array').focus()
return
}
disableInteractions()
let animationArr = getLinearSearchAnimations(searchNumber, [...array])
let lineContainerChildElements = Array.from(document.getElementById('lines-container').children)
let previous
for (let i = 0; i < animationArr.length; ++i) {
let animation = animationArr[i]
await sleep(100)
if (Number.isInteger(previous)) {
lineContainerChildElements[previous].style.backgroundColor = BG_COLOR
}
if (animation.found === true) {
lineContainerChildElements[animation.pos].style.backgroundColor = FOUND_COLOR
toast('success', 'Found on position: ' + animation.pos)
break
} else if (animation.found === false) {
lineContainerChildElements[animation.pos].style.backgroundColor = BG_COLOR
toast('fail', 'Not found')
enableInteractions()
break
} else {
lineContainerChildElements[animation.pos].style.backgroundColor = SELECT_COLOR
previous = animation.pos
}
}
enableInteractions()
}
async function doBinarySearch() {
if (!searchNumber) {
toast('fail', 'Enter search number!')
document.getElementById('search-number').focus()
return
}
if (!array.length) {
toast('fail', 'Generate Array!')
document.getElementById('generate-array').focus()
return
}
disableInteractions()
let lineContainerChildElements = Array.from(document.getElementById('lines-container').children).slice()
// Sort array and dom elements before performing binary search
array.sort((a, b) => a - b)
lineContainerChildElements.sort((a, b) => parseInt(a.style.height) - parseInt(b.style.height))
let lineContainerElement = document.getElementById('lines-container')
// Remove Present Children
let child = lineContainerElement.lastElementChild;
while (child) {
lineContainerElement.removeChild(child);
child = lineContainerElement.lastElementChild;
}
// Append the sorted childs
for (let i = 0; i < array.length; ++i) {
let lineElement = document.createElement('div')
lineElement.className = "bar"
lineElement.style.height = array[i] + 'px'
lineContainerElement.appendChild(lineElement)
}
await sleep(1000)
let animationArr = getBinarySearchAnimations(searchNumber, array)
let previous
lineContainerChildElements = Array.from(document.getElementById('lines-container').children)
for (let i = 0; i < animationArr.length; ++i) {
const animation = animationArr[i]
await sleep(300)
if (previous) {
if (previous.status === 'compare') {
lineContainerChildElements[previous.lowIndex].style.backgroundColor = BG_COLOR
previous.highIndex >= 0 && (lineContainerChildElements[previous.highIndex].style.backgroundColor = BG_COLOR)
} else {
lineContainerChildElements[previous.midIndex].style.backgroundColor = BG_COLOR
}
}
if (animation.status === 'found') {
lineContainerChildElements[animation.midIndex].style.backgroundColor = FOUND_COLOR
toast('success', 'Found at index: ' + animation.midIndex)
break
} else if (animation.status === 'not-found') {
lineContainerChildElements[animation.lowIndex].style.backgroundColor = BG_COLOR
lineContainerChildElements[animation.midIndex].style.backgroundColor = BG_COLOR
animation.highIndex >= 0 && (lineContainerChildElements[animation.highIndex].style.backgroundColor = BG_COLOR)
toast('fail', 'Not Found')
break
} else if (animation.status === 'compare') {
lineContainerChildElements[animation.lowIndex].style.backgroundColor = COMPARE_COLOR
animation.highIndex >= 0 && (lineContainerChildElements[animation.highIndex].style.backgroundColor = COMPARE_COLOR)
await sleep(300)
} else {
lineContainerChildElements[animation.midIndex].style.backgroundColor = SELECT_COLOR
}
previous = animation
}
enableInteractions()
}
async function doBubbleSort() {
if (!array.length) {
toast('fail', 'Generate Array!')
document.getElementById('generate-array').focus()
return
}
disableInteractions()
let lineContainerChildElements = Array.from(document.getElementById('lines-container').children)
let animationArr = getBubbleSortAnimations(array)
let previous
for (let i = 0; i < animationArr.length; ++i) {
await sleep(300)
if (previous) {
lineContainerChildElements[previous.posI].style.backgroundColor = BG_COLOR
lineContainerChildElements[previous.posJ].style.backgroundColor = BG_COLOR
}
const animation = animationArr[i]
if (animation.status === 'compare') {
lineContainerChildElements[animation.posI].style.backgroundColor = COMPARE_COLOR
lineContainerChildElements[animation.posJ].style.backgroundColor = COMPARE_COLOR
} else {
lineContainerChildElements[animation.posI].style.backgroundColor = SELECT_COLOR
lineContainerChildElements[animation.posJ].style.backgroundColor = SELECT_COLOR
await sleep(300)
const tempHeight = lineContainerChildElements[animation.posI].style.height
lineContainerChildElements[animation.posI].style.height = lineContainerChildElements[animation.posJ].style.height
lineContainerChildElements[animation.posJ].style.height = tempHeight
}
previous = animation
}
if (previous) {
lineContainerChildElements[previous.posI].style.backgroundColor = BG_COLOR
lineContainerChildElements[previous.posJ].style.backgroundColor = BG_COLOR
}
enableInteractions()
toast('success', 'Finshed sorting!')
}
async function doSelectionSort() {
if (!array.length) {
toast('fail', 'Generate Array!')
document.getElementById('generate-array').focus()
return
}
disableInteractions()
let lineContainerChildElements = Array.from(document.getElementById('lines-container').children)
let animationArr = getSelectionSortAnimations(array)
let previous
for (let i = 0; i < animationArr.length; ++i) {
await sleep(300)
if (previous) {
Number.isInteger(previous.min) && (lineContainerChildElements[previous.min].style.backgroundColor = BG_COLOR)
Number.isInteger(previous.j) && (lineContainerChildElements[previous.j].style.backgroundColor = BG_COLOR)
Number.isInteger(previous.i) && (lineContainerChildElements[previous.i].style.backgroundColor = BG_COLOR)
}
const animation = animationArr[i]
if (animation.status === 'compare') {
lineContainerChildElements[animation.min].style.backgroundColor = COMPARE_COLOR
lineContainerChildElements[animation.j].style.backgroundColor = COMPARE_COLOR
} else if (animation.status === 'swap') {
lineContainerChildElements[animation.min].style.backgroundColor = SELECT_COLOR
lineContainerChildElements[animation.i].style.backgroundColor = SELECT_COLOR
await sleep(300)
const tempHeight = lineContainerChildElements[animation.min].style.height
lineContainerChildElements[animation.min].style.height = lineContainerChildElements[animation.i].style.height
lineContainerChildElements[animation.i].style.height = tempHeight
} else {
lineContainerChildElements[animation.min].style.backgroundColor = FOUND_COLOR
}
previous = animation
}
if (previous) {
Number.isInteger(previous.min) && (lineContainerChildElements[previous.min].style.backgroundColor = BG_COLOR)
Number.isInteger(previous.j) && (lineContainerChildElements[previous.j].style.backgroundColor = BG_COLOR)
Number.isInteger(previous.i) && (lineContainerChildElements[previous.i].style.backgroundColor = BG_COLOR)
}
enableInteractions()
toast('success', 'Finshed sorting!')
}
</script>
</body>
</html>