-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
107 lines (99 loc) · 3.8 KB
/
main.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
numberset = []
animations = []
INITCOLOR = 'blue'
SWAPCOLOR = "red"
FINALCOLOR = "#90EE90" // light green
baseAnimationSpeed = 5
animationSpeed = baseAnimationSpeed
numOfBars = 150 // number of bars in the array
soundsOn = true
function resetArray(){
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
numOfBars = parseInt(document.getElementById("numofbars").value)
if (Number.isInteger(numOfBars) && numOfBars < vw/2 && numOfBars > 1){
numberset = []
txt = ""
width = Math.floor(vw / numOfBars)-2
margin = 1
if (width <= 1){
width = Math.floor(vw / numOfBars)
margin = 0
}
for (let i = 0; i < numOfBars; i++){
numberset.push(randomIntFromInterval(5, 600))
txt += "<div class='array-bar' style='height:"+numberset[i]+"px; width:"+width+"px; margin: 0 "+margin+"px'></div>"
}
document.getElementById("bars").innerHTML = txt
document.getElementById("numofbars").style.border = "none"
}
else{
document.getElementById("numofbars").style.border = "1px solid red"
return
}
}
function reversedArray(){
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
numOfBars = parseInt(document.getElementById("numofbars").value)
if (Number.isInteger(numOfBars) && numOfBars < vw/2 && numOfBars > 1){
numberset = []
txt = ""
width = Math.floor(vw / numOfBars)-2
margin = 1
if (width <= 1){
width = Math.floor(vw / numOfBars)
margin = 0
}
for (let i = numOfBars; i > 5; i--){
numberset.push(i)
}
for (let i = 0; i < numOfBars; i++){
txt += "<div class='array-bar' style='height:"+numberset[i]+"px; width:"+width+"px; margin: 0 "+margin+"px'></div>"
}
document.getElementById("bars").innerHTML = txt
document.getElementById("numofbars").style.border = "none"
}
else{
document.getElementById("numofbars").style.border = "1px solid red"
return
}
}
function enableButtons(){
document.getElementById("multiplier").disabled = false
document.getElementById("sorting-algorithm").disabled = false
document.getElementById("go").disabled = false
document.getElementById("reset").disabled = false
document.getElementById("reversedArray").disabled = false
document.getElementById("numofbars").disabled = false
}
function doSort(){
algorithm = document.getElementById("sorting-algorithm").value
multiplier = document.getElementById("multiplier").value
animationSpeed = baseAnimationSpeed * (1/multiplier)
document.getElementById("multiplier").disabled = true
document.getElementById("sorting-algorithm").disabled = true
document.getElementById("go").disabled = true
document.getElementById("reset").disabled = true
document.getElementById("reversedArray").disabled = true
document.getElementById("numofbars").disabled = true
if (algorithm == "merge"){doMergeSort()}
else if (algorithm == "heap"){doHeapSort()}
else if (algorithm == "hoare-quick"){doHoareQuickSort()}
else if (algorithm == "lomuto-quick"){doLomutoQuickSort()}
else if (algorithm == "bubble"){doBubbleSort()}
else if (algorithm == "insertion"){doInsertionSort()}
else if (algorithm == "selection"){doSelectionSort()}
else if (algorithm == "optimized-selection"){doOptimizedSelectionSort()}
}
function randomIntFromInterval(min, max){
return Math.floor(Math.random() * (max - min + 1) + min)
}
function toggleSounds(){
if (soundsOn){
document.getElementById("mute").innerHTML = "🔊"
soundsOn = false
}
else{
document.getElementById("mute").innerHTML = "🔈"
soundsOn = true
}
}