-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
260 lines (232 loc) · 7.1 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
<html>
<head>
<title>Algorithm Visualizer - Abhishek Kandi</title>
<style>
:root {
--font-family: Monospace;
--font-size: 1.5rem;
--button-font-size: 1rem;
}
.container {
padding:30px;
}
#user-preference{
padding:20px;
font-family: var(--font-family);
font-size: var(--font-size);
}
.user-preference-type{
display: inline;
padding: 10px;
}
#selected-array-size-elements{
padding: 20px;
margin: 100px auto;
}
.square{
padding:15px;
display: inline;
border-style: solid;
}
.current-iteration{
background-color: yellow;
}
.element-found{
background-color: lightgreen;
}
#algorithm-process-trigger{
border: none;
border-radius: 12px;
padding: 3px 10px;
color:white;
background-color: black;
font-family: var(--font-family);
font-size: var(--button-font-size);
outline:none;
}
</style>
</head>
<body>
<div class="container">
<div id="user-preference">
<div class="user-preference-type">
<label for="selected-algorithm">Algorithm </label>
<select id="selected-algorithm" onchange="changeInSelectedAlgorithm(value)">
<option value="linear-search">Linear Search</option>
<option value="binary-search">Binary Search</option>
<option value="selection-sort">Selection Sort</option>
<option value="bubble-sort">Bubble Sort</option>
</select>
</div>
<div class="user-preference-type">
<label for="array-size">Array size </label>
<select id="array-size" onchange="appendSelectedArraySizeElement(value)">
<option value=5>5</option>
<option value=10>10</option>
<option value=15>15</option>
<option value=20>20</option>
</select>
</div>
<div class="user-preference-type">
<label for="search-element">Search value </label>
</div>
<div class="user-preference-type">
<input type="text" id="search-element" name="search-element">
</div>
<div class="user-preference-type">
<button type="button" id="algorithm-process-trigger" onclick="startProcessing()">Find</button>
</div>
</div>
<div id="selected-array-size-elements">
</div>
</div>
</body>
<script>
document.onreadystatechange = function(){
var state = document.readyState;
if (state == 'complete') {
appendSelectedArraySizeElement(5)
}
}
function getSelectedAlgorithm(){
return document.getElementById("selected-algorithm").value
}
function getArraySize(){
return parseInt(document.getElementById("array-size").value)
}
function changeInSelectedAlgorithm(value){
if(value !== 'selection-sort' && value !== 'bubble-sort'){
appendSelectedArraySizeElement(getArraySize())
} else {
alert('Selected algorithm will be available soon :)')
document.getElementById("selected-algorithm").value = "linear-search"
}
}
function enableUserPreference(){
document.getElementById("selected-algorithm").disabled = false
document.getElementById("array-size").disabled = false
document.getElementById("search-element").disabled = false
document.getElementById("algorithm-process-trigger").disabled = false
}
function disableUserPreference(){
document.getElementById("selected-algorithm").disabled = true
document.getElementById("array-size").disabled = true
document.getElementById("search-element").disabled = true
document.getElementById("algorithm-process-trigger").disabled = true
}
async function startProcessing(){
let selectedAlgorithm = getSelectedAlgorithm()
let searchElement = document.getElementById("search-element").value
if(!searchElement){
alert('Please enter search element')
return;
}
disableUserPreference()
if(selectedAlgorithm == 'linear-search'){
await processLinearSearch(searchElement)
}
else{
await processBinarySearch(searchElement)
}
enableUserPreference()
}
function appendSelectedArraySizeElement(arraySize){
let selectedArraySizeElements = generateRandomNumbers(arraySize)
let selectedAlgorithm = getSelectedAlgorithm()
if(selectedAlgorithm === 'binary-search') selectedArraySizeElements.sort((a,b) => a - b)
let arrayHTML = ''
selectedArraySizeElements.forEach((element,index)=>{
let elementHTML = `<div class="square" id="square-${index}">${element}</div>`
arrayHTML += elementHTML
})
document.getElementById("selected-array-size-elements").innerHTML = arrayHTML
}
function generateRandomNumbers(arraySize) {
let randomNumbers = []
for(i = 0; randomNumbers.length != arraySize; i++){
let randomNum = Math.floor(Math.random() * (200 - 0) + 0);
if(!randomNumbers.includes()){
randomNumbers.push(randomNum)
}
}
return randomNumbers
}
function getArrayToBeLooped(){
let arr = []
let squares = document.getElementsByClassName("square")
for(i = 0; i < squares.length; i++){
let squareValue = parseInt(squares[i].innerText)
arr.push(squareValue)
}
return arr
}
function removeColorForCurrentIterationSquare(index){
let squareId = `square-${index}`
document.getElementById(squareId).classList.remove("current-iteration")
}
function removeColorForElementFoundSquare(index){
let squareId = `square-${index}`
document.getElementById(squareId).classList.remove("element-found")
}
function fillColorForCurrentIterationSquare(index){
let squareId = `square-${index}`
document.getElementById(squareId).classList.add("current-iteration")
}
function fillColorForElementFoundSquare(index){
let squareId = `square-${index}`
document.getElementById(squareId).classList.add("element-found")
alert('Element Found')
setTimeout(() =>{
removeColorForElementFoundSquare(index)
},3000)
}
async function processLinearSearch(searchElement){
let arrayToBeSearched = getArrayToBeLooped()
for(let i = 0; i < arrayToBeSearched.length; i++){
fillColorForCurrentIterationSquare(i)
await setTimer(1000)
if(arrayToBeSearched[i] == searchElement){
removeColorForCurrentIterationSquare(i)
fillColorForElementFoundSquare(i)
return i;
}
removeColorForCurrentIterationSquare(i)
}
promptElementNotFound();
}
function promptElementNotFound(){
alert('Element not found');
enableUserPreference()
}
function setTimer(ms){
return new Promise(res => setTimeout(res,ms))
}
async function processBinarySearch(searchElement) {
let sortedArray = getArrayToBeLooped()
var lowIndex = 0;
var highIndex = sortedArray.length - 1;
while (lowIndex <= highIndex) {
let initialLowIndex = lowIndex
let initialHighIndex = highIndex
fillColorForCurrentIterationSquare(initialLowIndex)
fillColorForCurrentIterationSquare(initialHighIndex)
await setTimer(1000)
var midIndex = Math.floor((lowIndex + highIndex) / 2);
if (sortedArray[midIndex] == searchElement) {
removeColorForCurrentIterationSquare(lowIndex)
removeColorForCurrentIterationSquare(highIndex)
await setTimer(300)
fillColorForElementFoundSquare(midIndex)
return midIndex;
} else if (sortedArray[midIndex] < searchElement) {
lowIndex = midIndex + 1;
} else {
highIndex = midIndex - 1;
}
removeColorForCurrentIterationSquare(initialLowIndex)
removeColorForCurrentIterationSquare(initialHighIndex)
}
promptElementNotFound();
}
</script>
</html>