├── .gitignore ├── .vscode └── settings.json ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── Sounds ├── beepSound.wav └── beepSound2.wav ├── Visualizer └── SortingVisualizer.js ├── index.js └── serviceWorker.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "git.ignoreLimitWarning": true 3 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sorting Visualizer 2 | 3 | This is a sorting visualizer I made to help me comprehend the similarities between all the sorting algorithms. 4 | I decided to include 5 algorithms ranging from worst time complexity to best. The following algorithms were 5 | included: 6 | - Bubble Sort (time complexity: O(n^2) 7 | - Heap Sort (time complexity: O(nlog(n)) -> My Personal Favorite 8 | - Merge Sort (time complexity: O(nlog(n)) 9 | - Quick Sort (time complexity: O(nlog(n)) 10 | - Radix Sort (time complexity: O(nk)) -> k = Amount of Digits in the value inside array with the largest amount of digits 11 | 12 | 13 | ## Time Taken: 14 | This took 1 week to complete. 15 | 16 | ## Technologies: 17 | - ReactJS -> Dependencies: Use-Sound, React-Toggle 18 | - CSS 19 | 20 | 21 | ## Available Scripts 22 | 23 | In the project directory, you can run: 24 | 25 | ### `npm start` 26 | 27 | Runs the app in the development mode.
28 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 29 | 30 | The page will reload if you make edits.
31 | You will also see any lint errors in the console. 32 | 33 | ### `npm test` 34 | 35 | Launches the test runner in the interactive watch mode.
36 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 37 | 38 | ### `npm run build` 39 | 40 | Builds the app for production to the `build` folder.
41 | It correctly bundles React in production mode and optimizes the build for the best performance. 42 | 43 | The build is minified and the filenames include the hashes.
44 | Your app is ready to be deployed! 45 | 46 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 47 | 48 | ### `npm run eject` 49 | 50 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 51 | 52 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 53 | 54 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 55 | 56 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sorting-visualizer", 3 | "version": "0.1.0", 4 | "private": true, 5 | "homepage": "http://machadop1407.github.io/Sorting-Visualizer", 6 | "dependencies": { 7 | "@testing-library/jest-dom": "^4.2.4", 8 | "@testing-library/react": "^9.5.0", 9 | "@testing-library/user-event": "^7.2.1", 10 | "react": "^16.13.1", 11 | "react-dom": "^16.13.1", 12 | "react-scripts": "3.4.1", 13 | "react-toggle": "^4.1.1", 14 | "use-sound": "^1.0.2" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "predeploy": "npm run build", 19 | "deploy": "gh-pages -d build", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": "react-app" 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | }, 39 | "devDependencies": { 40 | "gh-pages": "^3.1.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/Sorting-Visualizer/75be30f6593749c4824ba88af9c38ada6e9e5002/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/Sorting-Visualizer/75be30f6593749c4824ba88af9c38ada6e9e5002/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/Sorting-Visualizer/75be30f6593749c4824ba88af9c38ada6e9e5002/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | padding: 0; 3 | margin: 0; 4 | background-color: #f1f1f1; 5 | font-family: monospace; 6 | } 7 | 8 | button:focus { 9 | outline: none; 10 | } 11 | 12 | .sortingVisualizer { 13 | width: 100vw; 14 | height: 100vh; 15 | } 16 | 17 | .header { 18 | width: 100%; 19 | height: 100px; 20 | background-color: #ff7f50; 21 | border-bottom: 1px solid #ffffff; 22 | } 23 | 24 | .header .headerBttns { 25 | display: flex; 26 | justify-content: center; 27 | padding-top: 20px; 28 | } 29 | 30 | .header button { 31 | display: inline-block; 32 | margin-left: 50px; 33 | width: 200px; 34 | height: 50px; 35 | border: none; 36 | border-radius: 10px; 37 | background-color: #ffffff; 38 | color: coral; 39 | font-size: 20px; 40 | font-weight: 700; 41 | transition: all 0.5s ease; 42 | } 43 | 44 | .header button:hover { 45 | cursor: pointer; 46 | box-shadow: 2px 2px 10px #000000; 47 | } 48 | 49 | .arrayControllers { 50 | display: flex; 51 | margin-top: 20px; 52 | margin-left: 550px; 53 | } 54 | 55 | .arrayControllers #velocity { 56 | margin-left: 50px; 57 | } 58 | 59 | .arrayControllers button { 60 | width: 120px; 61 | height: 60px; 62 | border: none; 63 | border-radius: 7px; 64 | background-color: #6495ed; 65 | color: #ffffff; 66 | opacity: 0.7; 67 | font-size: 18px; 68 | font-weight: 700; 69 | margin-left: 10px; 70 | } 71 | 72 | .arrayControllers button:hover { 73 | cursor: pointer; 74 | opacity: 1; 75 | } 76 | 77 | .arrayControllers #selected { 78 | opacity: 1; 79 | } 80 | 81 | .arrayControllers #restart { 82 | background-color: #ff7f50; 83 | border: 1px solid white; 84 | opacity: 1; 85 | } 86 | 87 | .arrayControllers .toggle { 88 | margin-top: 15px; 89 | margin-left: 20px; 90 | padding-top: 0; 91 | } 92 | 93 | .arrayControllers .toggle #soundLabel { 94 | position: absolute; 95 | margin-left: 10px; 96 | margin-top: 5px; 97 | color: #6495ed; 98 | font-weight: 800; 99 | } 100 | 101 | .arrayContainer { 102 | position: absolute; 103 | bottom: 100px; 104 | left: 120px; 105 | } 106 | 107 | .sortingVisualizer .bar { 108 | width: 10px; 109 | background-color: #ff7f50; 110 | margin-left: 4px; 111 | display: inline-block; 112 | border-radius: 10px; 113 | } 114 | 115 | .algorithmInfo { 116 | position: absolute; 117 | bottom: 10px; 118 | left: 120px; 119 | width: 1400px; 120 | height: 70px; 121 | border: 5px solid #6495ed; 122 | border-radius: 5px; 123 | display: flex; 124 | justify-content: center; 125 | align-items: center; 126 | } 127 | 128 | .algorithmInfo div { 129 | margin-left: 30px; 130 | color: #ff7f50; 131 | font-size: 25px; 132 | font-weight: 800; 133 | } -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./App.css"; 3 | import SortingVisualizer from "./Visualizer/SortingVisualizer"; 4 | 5 | function App() { 6 | return ( 7 |
8 | 9 |
10 | ); 11 | } 12 | 13 | export default App; 14 | -------------------------------------------------------------------------------- /src/Sounds/beepSound.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/Sorting-Visualizer/75be30f6593749c4824ba88af9c38ada6e9e5002/src/Sounds/beepSound.wav -------------------------------------------------------------------------------- /src/Sounds/beepSound2.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machadop1407/Sorting-Visualizer/75be30f6593749c4824ba88af9c38ada6e9e5002/src/Sounds/beepSound2.wav -------------------------------------------------------------------------------- /src/Visualizer/SortingVisualizer.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, useRef } from "react"; 2 | import useSound from "use-sound"; 3 | import beepSound from "../Sounds/beepSound.wav"; 4 | import beepSound2 from "../Sounds/beepSound2.wav"; 5 | 6 | import Toggle from "react-toggle"; 7 | import "react-toggle/style.css"; 8 | 9 | //Change At Will 10 | const ARRAYSIZE = 100; 11 | 12 | export default function SortingVisualizer() { 13 | //Array Used to Sort 14 | const [primaryArray, setPrimaryArray] = useState([]); 15 | 16 | //Animation Speed 17 | const [animationSpeed, setAnimationSpeed] = useState(40); 18 | 19 | const [soundOn, setSoundOn] = useState(true); 20 | 21 | //AlgorithmChosen 22 | const [algorithm, setAlgorithm] = useState({}); 23 | 24 | // Sound Effects 25 | const [playBeep1] = useSound(beepSound, { volume: soundOn ? 0.15 : 0 }); 26 | const [playBeep2] = useSound(beepSound2, { volume: soundOn ? 0.05 : 0 }); 27 | 28 | //Initial Random Array 29 | useEffect(() => { 30 | randomizeArray(); 31 | }, []); 32 | 33 | /* Requires: Array size is set 34 | * Effect: Generates a random array with values from 20 to 400 and changes the array state 35 | */ 36 | function randomizeArray() { 37 | for (var i = 0; i < primaryArray.length; i++) { 38 | var bar = document.getElementById(i).style; 39 | bar.backgroundColor = "#ff7f50"; 40 | } 41 | var array = []; 42 | for (var i = 0; i < ARRAYSIZE; i++) { 43 | array.push(randomVals(20, 400)); 44 | } 45 | 46 | setPrimaryArray(array); 47 | } 48 | 49 | // Generates a random val between min and max 50 | function randomVals(min, max) { 51 | var randomVal = Math.floor(Math.random() * (max - min + 1) + min); 52 | return randomVal; 53 | } 54 | 55 | //SLEEP FUNCTION --> Used to slow loop iteration 56 | const sleep = (milliseconds) => { 57 | return new Promise((resolve) => setTimeout(resolve, milliseconds)); 58 | }; 59 | 60 | //ANIMATION FOR WHEN THE SORTING IS FINISHED 61 | async function finishedAnimation() { 62 | for (var i = 0; i < primaryArray.length; i++) { 63 | var bar = document.getElementById(i).style; 64 | bar.backgroundColor = "green"; 65 | playBeep1(); 66 | await sleep(animationSpeed); 67 | } 68 | } 69 | 70 | // ------------ ALGORITHMS ------------ // 71 | 72 | // Bubble Sort 73 | async function bubbleSort() { 74 | var currentArr = primaryArray; 75 | 76 | var sorted = false; 77 | 78 | setAlgorithm({ name: "Bubble Sort", timeComplexity: "O(n^2)" }); 79 | 80 | while (!sorted) { 81 | sorted = true; 82 | 83 | for (var i = 0; i < currentArr.length - 1; i++) { 84 | if (currentArr[i] > currentArr[i + 1]) { 85 | var swap1 = currentArr[i]; 86 | var swap2 = currentArr[i + 1]; 87 | currentArr[i] = swap2; 88 | currentArr[i + 1] = swap1; 89 | setPrimaryArray([...primaryArray, currentArr]); 90 | 91 | //Changes the Style while swapping 92 | let bar1 = document.getElementById(i).style; 93 | let bar2 = document.getElementById(i + 1).style; 94 | bar1.backgroundColor = "#DC143C"; 95 | bar2.backgroundColor = "#6A5ACD"; 96 | 97 | await sleep(animationSpeed); 98 | 99 | //Changes the Style back to original 100 | bar1.backgroundColor = "#FF7F50"; 101 | bar2.backgroundColor = "#FF7F50"; 102 | 103 | sorted = false; 104 | playBeep1(); 105 | } 106 | playBeep2(); 107 | } 108 | if (sorted) finishedAnimation(); 109 | } 110 | } 111 | 112 | //HeapSort 113 | async function heapSort() { 114 | var arr = primaryArray; 115 | 116 | var length = arr.length; 117 | var index = Math.floor(length / 2 - 1); //This is always the last parent in a heap 118 | var lastChild = length - 1; 119 | 120 | setAlgorithm({ name: "Heap Sort", timeComplexity: "O(nlog(n))" }); 121 | 122 | while (index >= 0) { 123 | heapify(arr, length, index); 124 | index--; 125 | 126 | setPrimaryArray([...primaryArray, arr]); 127 | 128 | //Changes the Style while swapping 129 | if (index >= 0) { 130 | let bar1 = document.getElementById(index).style; 131 | let bar2 = document.getElementById(index + 1).style; 132 | bar1.backgroundColor = "#DC143C"; 133 | bar2.backgroundColor = "#6A5ACD"; 134 | 135 | await sleep(animationSpeed); 136 | 137 | playBeep1(); 138 | //Changes the Style back to original 139 | bar1.backgroundColor = "#FF7F50"; 140 | bar2.backgroundColor = "#FF7F50"; 141 | } else { 142 | await sleep(animationSpeed); 143 | } 144 | } 145 | 146 | while (lastChild >= 0) { 147 | var swap1 = arr[0]; 148 | var swap2 = arr[lastChild]; 149 | 150 | arr[0] = swap2; 151 | arr[lastChild] = swap1; 152 | heapify(arr, lastChild, 0); 153 | lastChild--; 154 | playBeep2(); 155 | 156 | setPrimaryArray([...primaryArray, arr]); 157 | 158 | //Changes the Style while swapping 159 | if (index >= 0) { 160 | let bar1 = document.getElementById(lastChild).style; 161 | let bar2 = document.getElementById(0).style; 162 | bar1.backgroundColor = "#DC143C"; 163 | bar2.backgroundColor = "#6A5ACD"; 164 | 165 | //Changes the Style back to original 166 | bar1.backgroundColor = "#FF7F50"; 167 | bar2.backgroundColor = "#FF7F50"; 168 | } else { 169 | await sleep(animationSpeed); 170 | } 171 | } 172 | 173 | // setTimeout(finishedAnimation, 2500); 174 | finishedAnimation(); 175 | } 176 | 177 | async function heapify(arr, length, index) { 178 | //Represents larges node out of the three 179 | var largest = index; 180 | 181 | //Left Child 182 | var leftNode = index * 2 + 1; 183 | //Right Child 184 | var rightNode = leftNode + 1; 185 | 186 | //Check if left is largest, check if reached end 187 | if (arr[leftNode] > arr[largest] && leftNode < length) { 188 | largest = leftNode; 189 | } 190 | 191 | //Check if right is largest, check if reached end 192 | if (arr[rightNode] > arr[largest] && rightNode < length) { 193 | largest = rightNode; 194 | } 195 | 196 | //Check if parent is still largest, if not: perform a swap between the smallest and the largest 197 | if (largest != index) { 198 | var swap1 = arr[index]; 199 | var swap2 = arr[largest]; 200 | 201 | arr[index] = swap2; 202 | arr[largest] = swap1; 203 | 204 | heapify(arr, length, largest); 205 | } 206 | 207 | return arr; 208 | } 209 | 210 | /*Merge Sort 211 | * 212 | * Splits array recursively untill individual elements are set, 213 | * Merge everything back while sorting. 214 | * Time complexity = O(nlog(n)) 215 | * 216 | * Adapted from The following version By Clement Mihailescu : https://github.com/clementmihailescu/Sorting-Visualizer-Tutorial/blob/master/src/sortingAlgorithms/sortingAlgorithms.js 217 | */ 218 | 219 | function mergeSort(array) { 220 | setAlgorithm({ name: "Merge Sort", timeComplexity: "O(nlog(n))" }); 221 | if (array.length <= 1) return array; 222 | const auxiliaryArray = array.slice(); 223 | 224 | //Call helper function with current sliced array 225 | mergeSortHelper(array, 0, array.length - 1, auxiliaryArray); 226 | 227 | return array; 228 | } 229 | 230 | async function mergeSortHelper(mainArray, startIdx, endIdx, auxiliaryArray) { 231 | if (startIdx === endIdx) return; 232 | 233 | //Get sliced array and recursively divide it untill it has single element arrays 234 | const middleIdx = Math.floor((startIdx + endIdx) / 2); 235 | mergeSortHelper(auxiliaryArray, startIdx, middleIdx, mainArray); 236 | mergeSortHelper(auxiliaryArray, middleIdx + 1, endIdx, mainArray); 237 | 238 | await sleep(animationSpeed); 239 | playBeep2(); 240 | doMerge(mainArray, startIdx, middleIdx, endIdx, auxiliaryArray); 241 | } 242 | 243 | //Performs merging 244 | function doMerge(mainArray, startIdx, middleIdx, endIdx, auxiliaryArray) { 245 | let k = startIdx; 246 | let i = startIdx; 247 | let j = middleIdx + 1; 248 | 249 | while (i <= middleIdx && j <= endIdx) { 250 | playBeep1(); 251 | if (auxiliaryArray[i] <= auxiliaryArray[j]) { 252 | //Compare values and overwrite primary array with new sorted array 253 | mainArray[k++] = auxiliaryArray[i++]; 254 | setPrimaryArray([...primaryArray, mainArray]); 255 | let bar1 = document.getElementById(k).style; 256 | let bar2 = document.getElementById(i).style; 257 | bar1.backgroundColor = "#DC143C"; 258 | bar2.backgroundColor = "#6A5ACD"; 259 | 260 | setTimeout(() => { 261 | bar1.backgroundColor = "#ff7f50"; 262 | bar2.backgroundColor = "#ff7f50"; 263 | }, 800); 264 | } else { 265 | //Compare values and overwrite primary array with new sorted array 266 | mainArray[k++] = auxiliaryArray[j++]; 267 | setPrimaryArray([...primaryArray, mainArray]); 268 | let bar1 = document.getElementById(k).style; 269 | let bar2 = document.getElementById(i).style; 270 | bar1.backgroundColor = "#DC143C"; 271 | bar2.backgroundColor = "#6A5ACD"; 272 | setTimeout(() => { 273 | bar1.backgroundColor = "#ff7f50"; 274 | bar2.backgroundColor = "#ff7f50"; 275 | }, 200); 276 | } 277 | } 278 | 279 | mergeBack(i, j, k, middleIdx, endIdx, mainArray, auxiliaryArray); 280 | } 281 | 282 | //MergeBack and fill the sorted Array 283 | function mergeBack(i, j, k, midIndex, endIndex, mainArray, auxiliaryArray) { 284 | while (i <= midIndex) { 285 | mainArray[k++] = auxiliaryArray[i++]; 286 | setPrimaryArray([...primaryArray, mainArray]); 287 | } 288 | while (j <= endIndex) { 289 | mainArray[k++] = auxiliaryArray[j++]; 290 | setPrimaryArray([...primaryArray, mainArray]); 291 | } 292 | } 293 | 294 | /* 295 | * Quick Sort 296 | * 297 | * 298 | * 299 | * 300 | * 301 | * 302 | */ 303 | 304 | function quickSort() { 305 | setAlgorithm({ name: "Quick Sort", timeComplexity: "O(nlog(n))" }); 306 | var currentArr = primaryArray; 307 | var left = 0; 308 | var right = currentArr.length - 1; 309 | 310 | sort(currentArr, left, right); 311 | setTimeout(finishedAnimation, 2500); 312 | } 313 | 314 | async function sort(arr, left, right) { 315 | if (left < right) { 316 | var partitionIndex = partition(arr, left, right); 317 | 318 | setPrimaryArray([...primaryArray, arr]); 319 | await sleep(animationSpeed + 100); 320 | playBeep2(); 321 | sort(arr, left, partitionIndex - 1); 322 | sort(arr, partitionIndex + 1, right); 323 | } 324 | } 325 | 326 | function partition(arr, left, right) { 327 | var pivot = arr[right]; 328 | var i = left - 1; 329 | playBeep1(); 330 | for (var j = left; j < right; j++) { 331 | if (arr[j] < pivot) { 332 | i++; 333 | var temp = arr[i]; 334 | arr[i] = arr[j]; 335 | arr[j] = temp; 336 | 337 | let bar1 = document.getElementById(i).style; 338 | let bar2 = document.getElementById(j).style; 339 | bar1.backgroundColor = "#DC143C"; 340 | bar2.backgroundColor = "#6A5ACD"; 341 | 342 | setTimeout(() => { 343 | bar1.backgroundColor = "#ff7f50"; 344 | bar2.backgroundColor = "#ff7f50"; 345 | }, 200); 346 | 347 | setPrimaryArray([...primaryArray, arr]); 348 | } 349 | } 350 | 351 | var temp = arr[i + 1]; 352 | arr[i + 1] = arr[right]; 353 | arr[right] = temp; 354 | 355 | return i + 1; 356 | } 357 | 358 | /////RadixSort 359 | 360 | function radixCaller() { 361 | setAlgorithm({ name: "Radix Sort", timeComplexity: "O(log10(n))" }); 362 | var currentArr = primaryArray; 363 | radixSort(currentArr); 364 | } 365 | 366 | async function radixSort(arr) { 367 | const max = getMax(arr); // length of the max digit in the array 368 | 369 | for (let i = 0; i < max; i++) { 370 | let buckets = Array.from({ length: 10 }, () => []); 371 | for (let j = 0; j < arr.length; j++) { 372 | buckets[getPosition(arr[j], i)].push(arr[j]); // pushing into buckets 373 | var bar = document.getElementById(i).style; 374 | bar.backgroundColor = "#6A5ACD"; 375 | } 376 | 377 | await sleep(animationSpeed + 300); 378 | 379 | var animArr = []; 380 | for (var c = 0; c < ARRAYSIZE / 10; c++) { 381 | animArr.push(randomVals(0, ARRAYSIZE - 1)); 382 | } 383 | 384 | animArr.forEach((val) => { 385 | var bar = document.getElementById(val).style; 386 | bar.backgroundColor = "#DC143C"; 387 | playBeep1(); 388 | }); 389 | 390 | var animArr2 = []; 391 | for (var c = 0; c < ARRAYSIZE / 10; c++) { 392 | animArr2.push(randomVals(0, ARRAYSIZE - 1)); 393 | } 394 | 395 | animArr2.forEach((val) => { 396 | var bar = document.getElementById(val).style; 397 | bar.backgroundColor = "#6A5ACD"; 398 | playBeep2(); 399 | }); 400 | 401 | arr = [].concat(...buckets); 402 | setPrimaryArray(arr); 403 | } 404 | 405 | finishedAnimation(); 406 | 407 | return arr; 408 | } 409 | 410 | function getMax(arr) { 411 | let max = 0; 412 | for (let num of arr) { 413 | if (max < num.toString().length) { 414 | max = num.toString().length; 415 | } 416 | } 417 | return max; 418 | } 419 | 420 | function getPosition(num, place) { 421 | var result = Math.floor(Math.abs(num) / Math.pow(10, place)) % 10; 422 | return result; 423 | } 424 | 425 | return ( 426 |
427 |
428 | {/* Algorithm Buttons */} 429 |
430 | 431 | 432 | 433 | 441 | 442 | 443 |
444 |
445 | 446 |
447 | 456 |
457 | 465 | 473 | 481 |
482 |
483 | { 487 | setSoundOn(!soundOn); 488 | }} 489 | /> 490 | Sound {soundOn ? "On" : "Off"} 491 |
492 |
493 |
494 | {primaryArray && 495 | primaryArray.map((val, key) => { 496 | return ( 497 |
503 | ); 504 | })} 505 |
506 | 507 | {algorithm.name != undefined && ( 508 |
509 | <> 510 |
Algorithm: {algorithm.name}
511 |
512 | Time Complexity: {algorithm.timeComplexity}{" "} 513 |
514 | 515 |
516 | )} 517 |
518 | ); 519 | } 520 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import App from "./App"; 4 | import * as serviceWorker from "./serviceWorker"; 5 | 6 | ReactDOM.render( 7 | 8 | 9 | , 10 | document.getElementById("root") 11 | ); 12 | 13 | // If you want your app to work offline and load faster, you can change 14 | // unregister() to register() below. Note this comes with some pitfalls. 15 | // Learn more about service workers: https://bit.ly/CRA-PWA 16 | serviceWorker.unregister(); 17 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | --------------------------------------------------------------------------------