|
2 | 2 | <head>
|
3 | 3 | <title>Algorithm Visualizer - Abhishek Kandi</title>
|
4 | 4 | <style>
|
| 5 | + :root { |
| 6 | + --font-family: Monospace; |
| 7 | + --font-size: 1.5rem; |
| 8 | + --button-font-size: 1rem; |
| 9 | + } |
| 10 | + .container { |
| 11 | + padding:30px; |
| 12 | + } |
5 | 13 | #user-preference{
|
6 | 14 | padding:20px;
|
7 |
| - font-family: cursive; |
| 15 | + font-family: var(--font-family); |
| 16 | + font-size: var(--font-size); |
8 | 17 | }
|
9 | 18 | .user-preference-type{
|
10 | 19 | display: inline;
|
|
25 | 34 | .element-found{
|
26 | 35 | background-color: lightgreen;
|
27 | 36 | }
|
| 37 | + #algorithm-process-trigger{ |
| 38 | + border: none; |
| 39 | + border-radius: 12px; |
| 40 | + padding: 3px 10px; |
| 41 | + color:white; |
| 42 | + background-color: black; |
| 43 | + font-family: var(--font-family); |
| 44 | + font-size: var(--button-font-size); |
| 45 | + outline:none; |
| 46 | + } |
28 | 47 | </style>
|
29 | 48 | </head>
|
30 | 49 | <body>
|
31 |
| - <div id="user-preference"> |
32 |
| - <div class="user-preference-type"> |
33 |
| - <label for="selected-algorithm">Algorithm </label> |
34 |
| - <select id="selected-algorithm"> |
35 |
| - <option value="linear-search">Linear Search</option> |
36 |
| - <option value="binary-search">Binary Search</option> |
37 |
| - </select> |
38 |
| - </div> |
39 |
| - |
40 |
| - <div class="user-preference-type"> |
41 |
| - <label for="array-size">Array size </label> |
42 |
| - <select id="array-size" onchange="appendSelectedArraySizeElement(value)"> |
43 |
| - <option value=5>5</option> |
44 |
| - <option value=10>10</option> |
45 |
| - <option value=15>15</option> |
46 |
| - <option value=20>20</option> |
47 |
| - </select> |
48 |
| - </div> |
49 |
| - |
50 |
| - <div class="user-preference-type"> |
51 |
| - <label for="search-element">Search value </label> |
52 |
| - </div> |
53 |
| - |
54 |
| - <div class="user-preference-type"> |
55 |
| - <input type="text" id="search-element" name="search-element"> |
| 50 | + <div class="container"> |
| 51 | + <div id="user-preference"> |
| 52 | + <div class="user-preference-type"> |
| 53 | + <label for="selected-algorithm">Algorithm </label> |
| 54 | + <select id="selected-algorithm" onchange="changeInSelectedAlgorithm(value)"> |
| 55 | + <option value="linear-search">Linear Search</option> |
| 56 | + <option value="binary-search">Binary Search</option> |
| 57 | + </select> |
| 58 | + </div> |
| 59 | + |
| 60 | + <div class="user-preference-type"> |
| 61 | + <label for="array-size">Array size </label> |
| 62 | + <select id="array-size" onchange="appendSelectedArraySizeElement(value)"> |
| 63 | + <option value=5>5</option> |
| 64 | + <option value=10>10</option> |
| 65 | + <option value=15>15</option> |
| 66 | + <option value=20>20</option> |
| 67 | + </select> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div class="user-preference-type"> |
| 71 | + <label for="search-element">Search value </label> |
| 72 | + </div> |
| 73 | + |
| 74 | + <div class="user-preference-type"> |
| 75 | + <input type="text" id="search-element" name="search-element"> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div class="user-preference-type"> |
| 79 | + <button type="button" id="algorithm-process-trigger" onclick="startProcessing()">Find</button> |
| 80 | + </div> |
56 | 81 | </div>
|
57 |
| - |
58 |
| - <div class="user-preference-type"> |
59 |
| - <button type="button" onclick="startProcessing()">Start Magic!</button> |
| 82 | + |
| 83 | + <div id="selected-array-size-elements"> |
| 84 | + |
60 | 85 | </div>
|
61 | 86 | </div>
|
62 |
| - |
63 |
| - <div id="selected-array-size-elements"> |
64 |
| - |
65 |
| - </div> |
66 | 87 | </body>
|
67 | 88 | <script>
|
68 | 89 | document.onreadystatechange = function(){
|
|
72 | 93 | }
|
73 | 94 | }
|
74 | 95 |
|
75 |
| - function startProcessing(){ |
76 |
| - let selectedAlgorithm = document.getElementById("selected-algorithm").value |
| 96 | + function getSelectedAlgorithm(){ |
| 97 | + return document.getElementById("selected-algorithm").value |
| 98 | + } |
| 99 | + |
| 100 | + function getArraySize(){ |
| 101 | + return parseInt(document.getElementById("array-size").value) |
| 102 | + } |
| 103 | + |
| 104 | + function changeInSelectedAlgorithm(value){ |
| 105 | + appendSelectedArraySizeElement(getArraySize()) |
| 106 | + } |
| 107 | + |
| 108 | + function enableUserPreference(){ |
| 109 | + document.getElementById("selected-algorithm").disabled = false |
| 110 | + document.getElementById("array-size").disabled = false |
| 111 | + document.getElementById("search-element").disabled = false |
| 112 | + document.getElementById("algorithm-process-trigger").disabled = false |
| 113 | + } |
| 114 | + |
| 115 | + function disableUserPreference(){ |
| 116 | + document.getElementById("selected-algorithm").disabled = true |
| 117 | + document.getElementById("array-size").disabled = true |
| 118 | + document.getElementById("search-element").disabled = true |
| 119 | + document.getElementById("algorithm-process-trigger").disabled = true |
| 120 | + } |
| 121 | + |
| 122 | + async function startProcessing(){ |
| 123 | + let selectedAlgorithm = getSelectedAlgorithm() |
77 | 124 | let searchElement = document.getElementById("search-element").value
|
78 | 125 |
|
79 | 126 | if(!searchElement){
|
80 | 127 | alert('Please enter search element')
|
81 | 128 | return;
|
82 | 129 | }
|
83 | 130 |
|
84 |
| - if(selectedAlgorithm == 'linear-search') |
85 |
| - processLinearSearch(searchElement) |
| 131 | + disableUserPreference() |
| 132 | + if(selectedAlgorithm == 'linear-search'){ |
| 133 | + await processLinearSearch(searchElement) |
| 134 | + } |
| 135 | + else{ |
| 136 | + await processBinarySearch(searchElement) |
| 137 | + } |
| 138 | + enableUserPreference() |
| 139 | + |
86 | 140 | }
|
87 | 141 |
|
88 | 142 | function appendSelectedArraySizeElement(arraySize){
|
89 | 143 | let selectedArraySizeElements = generateRandomNumbers(arraySize)
|
| 144 | + let selectedAlgorithm = getSelectedAlgorithm() |
| 145 | + if(selectedAlgorithm === 'binary-search') selectedArraySizeElements.sort((a,b) => a - b) |
90 | 146 | let arrayHTML = ''
|
91 | 147 | selectedArraySizeElements.forEach((element,index)=>{
|
92 | 148 | let elementHTML = `<div class="square" id="square-${index}">${element}</div>`
|
|
134 | 190 | function fillColorForElementFoundSquare(index){
|
135 | 191 | let squareId = `square-${index}`
|
136 | 192 | document.getElementById(squareId).classList.add("element-found")
|
| 193 | + alert('Element Found') |
137 | 194 | setTimeout(() =>{
|
138 | 195 | removeColorForElementFoundSquare(index)
|
139 | 196 | },3000)
|
140 | 197 | }
|
141 | 198 |
|
142 | 199 | async function processLinearSearch(searchElement){
|
143 |
| - |
144 | 200 | let arrayToBeSearched = getArrayToBeLooped()
|
145 | 201 | for(let i = 0; i < arrayToBeSearched.length; i++){
|
146 | 202 | fillColorForCurrentIterationSquare(i)
|
147 |
| - await timer(1000) |
| 203 | + await setTimer(1000) |
148 | 204 | if(arrayToBeSearched[i] == searchElement){
|
149 | 205 | removeColorForCurrentIterationSquare(i)
|
150 | 206 | fillColorForElementFoundSquare(i)
|
151 | 207 | return i;
|
152 | 208 | }
|
153 | 209 | removeColorForCurrentIterationSquare(i)
|
154 | 210 | }
|
| 211 | + promptElementNotFound(); |
| 212 | + } |
155 | 213 |
|
| 214 | + function promptElementNotFound(){ |
156 | 215 | alert('Element not found');
|
| 216 | + enableUserPreference() |
157 | 217 | }
|
158 | 218 |
|
159 |
| - function timer(ms){ |
| 219 | + function setTimer(ms){ |
160 | 220 | return new Promise(res => setTimeout(res,ms))
|
161 | 221 | }
|
162 | 222 |
|
163 |
| - // function processLinearSearch(searchElement){ |
164 | 223 |
|
165 |
| - // let arrayToBeSearched = getArrayToBeLooped() |
166 |
| - // for(i = 0; i < arrayToBeSearched.length; i++){ |
167 |
| - // fillColorForCurrentIterationSquare(i) |
168 |
| - // if(arrayToBeSearched[i] == searchElement){ |
169 |
| - // removeColorForCurrentIterationSquare(i) |
170 |
| - // fillColorForElementFoundSquare(i) |
171 |
| - // return i; |
172 |
| - // } |
173 |
| - // removeColorForCurrentIterationSquare(i) |
174 |
| - // } |
175 |
| - |
176 |
| - // return -1; |
177 |
| - // } |
| 224 | + async function processBinarySearch(searchElement) { |
| 225 | + let sortedArray = getArrayToBeLooped() |
| 226 | + var lowIndex = 0; |
| 227 | + var highIndex = sortedArray.length - 1; |
| 228 | + while (lowIndex <= highIndex) { |
| 229 | + let initialLowIndex = lowIndex |
| 230 | + let initialHighIndex = highIndex |
| 231 | + fillColorForCurrentIterationSquare(initialLowIndex) |
| 232 | + fillColorForCurrentIterationSquare(initialHighIndex) |
| 233 | + await setTimer(1000) |
| 234 | + var midIndex = Math.floor((lowIndex + highIndex) / 2); |
| 235 | + if (sortedArray[midIndex] == searchElement) { |
| 236 | + removeColorForCurrentIterationSquare(lowIndex) |
| 237 | + removeColorForCurrentIterationSquare(highIndex) |
| 238 | + await setTimer(300) |
| 239 | + fillColorForElementFoundSquare(midIndex) |
| 240 | + return midIndex; |
| 241 | + } else if (sortedArray[midIndex] < searchElement) { |
| 242 | + lowIndex = midIndex + 1; |
| 243 | + } else { |
| 244 | + highIndex = midIndex - 1; |
| 245 | + } |
| 246 | + removeColorForCurrentIterationSquare(initialLowIndex) |
| 247 | + removeColorForCurrentIterationSquare(initialHighIndex) |
| 248 | + } |
| 249 | + |
| 250 | + promptElementNotFound(); |
| 251 | + } |
178 | 252 | </script>
|
179 | 253 | </html>
|
0 commit comments