├── public ├── favicon.ico ├── logo192.png ├── logo512.png ├── robots.txt ├── manifest.json └── index.html ├── src ├── store.js ├── reducer │ ├── index.js │ └── updateProps.js ├── components │ ├── Home.css │ ├── Home.js │ ├── Header.js │ ├── Header.css │ ├── Sidebar.css │ ├── Visuals.css │ ├── SelectionSort.js │ ├── MergeSort.js │ ├── BubbleSort.js │ ├── QuickSort.js │ ├── InsertionSort.js │ ├── Visuals.js │ └── Sidebar.js ├── App.js ├── index.js ├── index.css └── App.css ├── .gitignore ├── package.json └── README.md /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dasharath9920/AlgoVisuals/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dasharath9920/AlgoVisuals/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dasharath9920/AlgoVisuals/HEAD/public/logo512.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/store.js: -------------------------------------------------------------------------------- 1 | import { createStore } from "redux"; 2 | import rootReducer from "./reducer"; 3 | 4 | const store = createStore(rootReducer); 5 | 6 | export default store; 7 | -------------------------------------------------------------------------------- /src/reducer/index.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from "redux"; 2 | import updateProps from "./updateProps"; 3 | 4 | const rootReducer = combineReducers({updateProps}); 5 | 6 | export default rootReducer; -------------------------------------------------------------------------------- /src/components/Home.css: -------------------------------------------------------------------------------- 1 | .home { 2 | display: flex; 3 | height: 100%; 4 | } 5 | 6 | @media only screen and (max-width: 600px) { 7 | .home { 8 | flex-direction: column; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/components/Home.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import './Home.css'; 3 | import Sidebar from './Sidebar'; 4 | import Visuals from './Visuals'; 5 | 6 | function Home() { 7 | return ( 8 |
9 | 10 | 11 |
12 | ) 13 | } 14 | 15 | export default Home; -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import './App.css'; 2 | import Header from './components/Header'; 3 | import Home from './components/Home'; 4 | 5 | function App() { 6 | return ( 7 |
8 |
9 |
10 | 11 |
12 |
13 | ); 14 | } 15 | 16 | export default App; 17 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import store from './store'; 6 | import { Provider } from 'react-redux'; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | 12 | 13 | , 14 | document.getElementById('root') 15 | ); -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | * { 2 | margin: 0; 3 | padding: 0; 4 | transition: 0.3s ease all; 5 | } 6 | 7 | body { 8 | margin: 0; 9 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 10 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 11 | sans-serif; 12 | -webkit-font-smoothing: antialiased; 13 | -moz-osx-font-smoothing: grayscale; 14 | } 15 | 16 | code { 17 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 18 | monospace; 19 | } 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | display: flex; 3 | align-items: center; 4 | justify-content: center; 5 | background-color: rgb(255, 253, 111); 6 | width: 100%; 7 | height: 100vh; 8 | } 9 | 10 | .container { 11 | width: 80%; 12 | height: 90%; 13 | border-radius: 3px; 14 | background-color: white; 15 | overflow: hidden; 16 | box-shadow: 0px 0px 10px 1px grey; 17 | /* min-width: 700px; */ 18 | position: relative; 19 | } 20 | 21 | @media only screen and (max-width: 900px) { 22 | .container { 23 | width: 90%; 24 | } 25 | } 26 | 27 | @media only screen and (max-width: 600px) { 28 | .container { 29 | width: 100%; 30 | height: 100%; 31 | } 32 | 33 | .App { 34 | align-items: flex-start; 35 | background-color: rgb(0, 255, 255); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sorting_visuals", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@emotion/react": "^11.8.1", 7 | "@emotion/styled": "^11.8.1", 8 | "@mui/material": "^5.5.0", 9 | "@testing-library/jest-dom": "^5.16.2", 10 | "@testing-library/react": "^12.1.4", 11 | "@testing-library/user-event": "^13.5.0", 12 | "react": "^17.0.2", 13 | "react-dom": "^17.0.2", 14 | "react-redux": "^7.2.6", 15 | "react-scripts": "5.0.0", 16 | "redux": "^4.1.2", 17 | "web-vitals": "^2.1.4" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": [ 27 | "react-app", 28 | "react-app/jest" 29 | ] 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/components/Header.js: -------------------------------------------------------------------------------- 1 | import React,{useState,useEffect} from 'react'; 2 | import './Header.css'; 3 | 4 | function Header() { 5 | 6 | const [showHeaderBar, setShowHeaderBar] = useState(true); 7 | 8 | const contactHandler = () => { 9 | setShowHeaderBar(!showHeaderBar); 10 | } 11 | 12 | return ( 13 |
14 |
15 |

Frontend Developer

16 |
17 | 18 | 19 | 20 | 21 |
22 |
23 | 24 |
AlgoVisuals
25 |
26 |

Get In Touch

27 |
28 |
29 | ) 30 | } 31 | 32 | export default Header; -------------------------------------------------------------------------------- /src/components/Header.css: -------------------------------------------------------------------------------- 1 | .header { 2 | height: 60px; 3 | display: flex; 4 | align-items: center; 5 | justify-content: space-between; 6 | background-color: #81e2ff; 7 | padding: 0px 80px; 8 | } 9 | 10 | .header span { 11 | color: crimson; 12 | } 13 | 14 | .header-logo { 15 | font-family: "Times New Roman", Times, serif; 16 | font-size: 2rem; 17 | letter-spacing: 1px; 18 | font-weight: bold; 19 | } 20 | 21 | .header__options { 22 | display: flex; 23 | } 24 | 25 | a { 26 | text-decoration: none; 27 | } 28 | 29 | .header__option { 30 | font-weight: 500; 31 | margin-left: 100px; 32 | color: rgb(97, 97, 97); 33 | cursor: pointer; 34 | } 35 | 36 | #header__bar { 37 | position: absolute; 38 | right: 0px; 39 | bottom: 0px; 40 | height: 120px; 41 | width: 100%; 42 | color: white; 43 | background-color: rgb(245, 148, 21); 44 | transition: 0.5s ease all; 45 | z-index: 300; 46 | display: flex; 47 | align-items: center; 48 | justify-content: center; 49 | flex-direction: column; 50 | } 51 | 52 | .header__bar__title { 53 | font-size: 2rem; 54 | margin-bottom: 10px; 55 | } 56 | 57 | .header__bar__icons { 58 | color: white; 59 | } 60 | 61 | #header__bar img { 62 | height: 30px; 63 | width: 30px; 64 | margin: 0px 10px; 65 | } 66 | 67 | @media only screen and (max-width: 600px) { 68 | .header-logo { 69 | margin-right: -50px; 70 | } 71 | 72 | .header { 73 | padding: 0px 10px; 74 | } 75 | 76 | .header__option { 77 | font-size: 0.9rem; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/reducer/updateProps.js: -------------------------------------------------------------------------------- 1 | import { ActionTypes } from "@mui/base"; 2 | 3 | const initializer = { 4 | algorithm: 'bubble', 5 | color: '#35c6ff', 6 | speed: 100, 7 | range: '30', 8 | play: false, 9 | values: [], 10 | timeouts:[] 11 | }; 12 | 13 | const updateProps = (state=initializer,action) => { 14 | switch(action.type){ 15 | 16 | case 'UPDATE_RANGE':{ 17 | let arr=[]; 18 | for(let i = 0; i < action.range; i++) 19 | arr.push([Math.floor(Math.random()*100)+1,i]); 20 | 21 | return {...state, range:action.range, values:arr}; 22 | } 23 | 24 | case 'UPDATE_VALUES': { 25 | return {...state,values:action._values}; 26 | } 27 | 28 | case 'UPDATE_SPEED':{ 29 | return {...state, speed:action.speed}; 30 | } 31 | 32 | case 'UPDATE_COLOR':{ 33 | return {...state, color:action.color}; 34 | } 35 | 36 | case 'UPDATE_ALGORITHM':{ 37 | return {...state, algorithm:action.algorithm}; 38 | } 39 | 40 | case 'UPDATE_TIEMOUTS':{ 41 | return {...state, timeouts:action._timeouts}; 42 | } 43 | 44 | case 'CHANGE_VALUES':{ 45 | let arr=[],range = state.range; 46 | for(let i = 0; i < range; i++) 47 | arr.push([Math.floor(Math.random()*100)+1, i]); 48 | return {...state,values:arr}; 49 | } 50 | 51 | case 'PLAY_PAUSE': { 52 | return {...state,play:action._play}; 53 | } 54 | 55 | default: 56 | return state; 57 | } 58 | } 59 | 60 | export default updateProps; -------------------------------------------------------------------------------- /src/components/Sidebar.css: -------------------------------------------------------------------------------- 1 | .sidebar { 2 | width: 280px; 3 | box-shadow: 5px 0px 10px 1px lightgrey; 4 | background-color: aliceblue; 5 | } 6 | 7 | .sidebar__option { 8 | padding: 20px; 9 | height: 40px; 10 | display: flex; 11 | align-items: center; 12 | justify-content: space-between; 13 | border-bottom: 1px solid rgb(238, 238, 238); 14 | } 15 | 16 | label { 17 | font-weight: 500; 18 | } 19 | 20 | select { 21 | border: none; 22 | font-size: 0.9rem; 23 | outline: none; 24 | background-color: transparent; 25 | } 26 | 27 | .MuiSlider-track { 28 | color: green; 29 | } 30 | 31 | .slider { 32 | display: flex; 33 | align-items: center; 34 | justify-content: center; 35 | } 36 | 37 | @media only screen and (max-width: 1000px) { 38 | .sidebar { 39 | width: 200px; 40 | } 41 | 42 | .sidebar__option { 43 | padding: 10px; 44 | } 45 | 46 | select { 47 | font-size: 0.8rem; 48 | } 49 | 50 | label { 51 | font-size: 0.9rem; 52 | } 53 | } 54 | 55 | @media only screen and (max-width: 600px) { 56 | .sidebar { 57 | height: 40px; 58 | width: 100%; 59 | display: flex; 60 | align-items: center; 61 | justify-content: space-between; 62 | box-shadow: 0px 2px 10px 1px lightgrey; 63 | } 64 | 65 | .slider { 66 | max-width: 50px; 67 | } 68 | .sidebar__option { 69 | padding: 10px; 70 | margin-bottom: 10px; 71 | display: flex; 72 | align-items: center; 73 | justify-content: center; 74 | margin: 2px; 75 | max-width: 72px; 76 | margin-right: -2px; 77 | border-bottom: none; 78 | } 79 | 80 | select { 81 | font-size: 0.8rem; 82 | font-weight: 500; 83 | } 84 | 85 | label { 86 | font-size: 0.9rem; 87 | display: none; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/components/Visuals.css: -------------------------------------------------------------------------------- 1 | .visuals { 2 | flex: 1; 3 | padding: 20px; 4 | display: flex; 5 | align-items: center; 6 | justify-content: flex-start; 7 | flex-direction: column; 8 | } 9 | 10 | .visualizer { 11 | width: 100%; 12 | display: flex; 13 | align-items: center; 14 | justify-content: center; 15 | position: relative; 16 | } 17 | 18 | .legend { 19 | position: absolute; 20 | left: 0px; 21 | top: 0px; 22 | padding: 2px 5px; 23 | border: 1px solid grey; 24 | display: flex; 25 | align-items: center; 26 | font-size: 0.8rem; 27 | font-weight: 500; 28 | } 29 | 30 | .legend__lable { 31 | height: 5px; 32 | width: 20px; 33 | background-color: black; 34 | margin-right: 3px; 35 | } 36 | 37 | .visual__items { 38 | display: flex; 39 | align-items: flex-end; 40 | justify-content: flex-start; 41 | height: 340px; 42 | margin-bottom: 40px; 43 | border-bottom: 1px solid black; 44 | position: relative; 45 | } 46 | 47 | .visual__item { 48 | position: absolute; 49 | } 50 | 51 | .visual__item > h4 { 52 | font-size: 0.4rem; 53 | } 54 | 55 | .visual__btns > button { 56 | width: 150px; 57 | background-color: rgb(0, 149, 199); 58 | padding: 7px 10px; 59 | border: none; 60 | border-radius: 5px; 61 | margin: 10px; 62 | font-size: 0.8rem; 63 | font-weight: bold; 64 | color: white; 65 | text-transform: uppercase; 66 | cursor: pointer; 67 | } 68 | 69 | @media only screen and (max-width: 900px) { 70 | .visuals { 71 | padding: 5px; 72 | } 73 | } 74 | 75 | @media only screen and (max-width: 600px) { 76 | .visual__btns { 77 | display: flex; 78 | margin-top: -20px; 79 | } 80 | 81 | .visuals { 82 | padding-top: 10px; 83 | } 84 | 85 | .visual__btns > button { 86 | margin-bottom: -3px; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/components/SelectionSort.js: -------------------------------------------------------------------------------- 1 | import React,{useEffect} from 'react'; 2 | import { useDispatch, useSelector } from 'react-redux'; 3 | 4 | const SelectionSort = () =>{ 5 | const myState = useSelector(state => state.updateProps); 6 | const dispatch = useDispatch(); 7 | 8 | let values = myState.values.map((item) => item[0]); 9 | let ids = myState.values.map((item) => item[1]); 10 | 11 | const solve = () => { 12 | let n = values.length; 13 | 14 | for(let i = 0; i < n; i++){ 15 | 16 | setTimeout(() => { 17 | let ind = i; 18 | for(let j = i; j < n; j++){ 19 | if(values[ind] > values[j]) 20 | ind = j; 21 | } 22 | let temp = values[i]; 23 | values[i] = values[ind]; 24 | values[ind] = temp; 25 | 26 | temp = ids[i]; 27 | ids[i] = ids[ind]; 28 | ids[ind] = temp; 29 | 30 | document.getElementById(ids[i]).style.transform = `translateX(${i*11}px)`; 31 | document.getElementById(ids[ind]).style.transform = `translateX(${ind*11}px)`; 32 | 33 | document.getElementById(ids[i]).childNodes[1].style.backgroundColor = 'black'; 34 | 35 | setTimeout(() => { 36 | document.getElementById(ids[i]).childNodes[1].style.backgroundColor = myState.color; 37 | },myState.speed*2); 38 | 39 | },i*myState.speed*3); 40 | } 41 | 42 | setTimeout(() => { 43 | dispatch({ 44 | type:'PLAY_PAUSE', 45 | _play:false 46 | }) 47 | 48 | dispatch({ 49 | type:'UPDATE_COLOR', 50 | color: 'rgb(0, 182, 0)' 51 | }) 52 | 53 | },(myState.speed*3*n)+50); 54 | }; 55 | 56 | useEffect(() => { 57 | if(myState.algorithm==='selection'){ 58 | if(myState.play) 59 | solve(); 60 | } 61 | },[myState.play]); 62 | 63 | return <>; 64 | } 65 | 66 | export default SelectionSort; -------------------------------------------------------------------------------- /src/components/MergeSort.js: -------------------------------------------------------------------------------- 1 | import React,{useEffect} from 'react'; 2 | import { useDispatch, useSelector } from 'react-redux'; 3 | 4 | const MergeSort = () => { 5 | const myState = useSelector(state => state.updateProps); 6 | const dispatch = useDispatch(); 7 | 8 | let values = myState.values.map((item) => item[0]); 9 | let ids = myState.values.map((item) => item[1]); 10 | 11 | const mergeSort = (values,ids,timer,l,r) => { 12 | if(l >= r) 13 | return; 14 | 15 | let mid = Math.floor((l+r)/2); 16 | 17 | mergeSort(values,ids,timer-1,l,mid); 18 | mergeSort(values,ids,timer-1,mid+1,r); 19 | 20 | setTimeout(() => { 21 | let color = []; 22 | for(let i = 0; i < 3; i++) 23 | color.push(Math.floor(Math.random()*200)); 24 | 25 | for(let i = l; i <= r; i++) 26 | document.getElementById(ids[i]).childNodes[1].style.backgroundColor = `rgb(${color[0]},${color[1]},${color[2]})`; 27 | 28 | for(let i = l; i <= r; i++){ 29 | for(let j = i+1; j <= r; j++){ 30 | 31 | if(values[i]>values[j]){ 32 | [values[i],values[j]] = [values[j],values[i]]; 33 | [ids[i],ids[j]] = [ids[j],ids[i]]; 34 | 35 | let new_ids = [...ids]; 36 | 37 | document.getElementById(new_ids[i]).style.transform = `translateX(${i*11}px)`; 38 | document.getElementById(new_ids[j]).style.transform = `translateX(${j*11}px)`; 39 | } 40 | } 41 | } 42 | },timer*myState.speed*5); 43 | } 44 | 45 | const solve = () => { 46 | mergeSort(values,ids,Math.ceil(Math.log(values.length+1)),0,values.length-1); 47 | 48 | setTimeout(() => { 49 | dispatch({ 50 | type:'PLAY_PAUSE', 51 | _play:false 52 | }) 53 | 54 | dispatch({ 55 | type:'UPDATE_COLOR', 56 | color: 'rgb(0, 182, 0)' 57 | }) 58 | },5*myState.speed*(1+Math.ceil(Math.log(values.length+1)))+50); 59 | }; 60 | 61 | useEffect(() => { 62 | if(myState.algorithm==='merge'){ 63 | if(myState.play) 64 | solve(); 65 | } 66 | },[myState.play]); 67 | 68 | return <>; 69 | } 70 | 71 | export default MergeSort; -------------------------------------------------------------------------------- /src/components/BubbleSort.js: -------------------------------------------------------------------------------- 1 | import React,{useEffect, useState} from 'react'; 2 | import { useDispatch, useSelector } from 'react-redux'; 3 | 4 | const BubbleSort = () =>{ 5 | const myState = useSelector(state => state.updateProps); 6 | const dispatch = useDispatch(); 7 | 8 | let values = myState.values.map((item) => item[0]); 9 | let ids = myState.values.map((item) => item[1]); 10 | 11 | const solve = () => { 12 | 13 | for(let i = values.length,timer = 0; i > 0;timer += i-1, i--){ 14 | setTimeout(() => { 15 | for(let j = 1; j < i; j++){ 16 | setTimeout(() => { 17 | document.getElementById(ids[j]).childNodes[1].style.backgroundColor = 'black'; 18 | document.getElementById(ids[j-1]).childNodes[1].style.backgroundColor = 'black'; 19 | 20 | setTimeout(() => { 21 | document.getElementById(ids[j]).childNodes[1].style.backgroundColor = myState.color; 22 | document.getElementById(ids[j-1]).childNodes[1].style.backgroundColor = myState.color; 23 | },myState.speed-10); 24 | 25 | if(values[j] { 46 | dispatch({ 47 | type:'PLAY_PAUSE', 48 | _play:false 49 | }) 50 | 51 | dispatch({ 52 | type:'UPDATE_COLOR', 53 | color: 'rgb(0, 182, 0)' 54 | }) 55 | 56 | },(((myState.values.length-1)*(myState.values.length))/2)*myState.speed+50); 57 | } 58 | 59 | useEffect(() => { 60 | if(myState.algorithm==='bubble'){ 61 | if(myState.play) 62 | solve(); 63 | } 64 | },[myState.play]); 65 | 66 | return <>; 67 | } 68 | 69 | export default BubbleSort; -------------------------------------------------------------------------------- /src/components/QuickSort.js: -------------------------------------------------------------------------------- 1 | import React,{useEffect, useState} from 'react'; 2 | import { useDispatch, useSelector } from 'react-redux'; 3 | 4 | const QuickSort = () =>{ 5 | const myState = useSelector(state => state.updateProps); 6 | const dispatch = useDispatch(); 7 | 8 | let values = myState.values.map((item) => item[0]); 9 | let ids = myState.values.map((item) => item[1]); 10 | 11 | const swap = (arr,i,j) => { 12 | let temp = arr[i]; 13 | arr[i] = arr[j]; 14 | arr[j] = temp; 15 | } 16 | 17 | const partition = (values,ids,l,r,timer) => { 18 | let pivot = values[r]; 19 | 20 | let j = l-1; 21 | for(let i = l; i < r; i++){ 22 | if(values[i] { 34 | 35 | document.getElementById(ids[j+1]).childNodes[1].style.backgroundColor = 'black'; 36 | setTimeout(() => { 37 | document.getElementById(ids[j+1]).childNodes[1].style.backgroundColor = myState.color; 38 | },myState.speed*6-10) 39 | 40 | document.getElementById(ids[r]).style.transform = `translateX(${r*11}px)`; 41 | document.getElementById(ids[j+1]).style.transform = `translateX(${(j+1)*11}px)`; 42 | 43 | },myState.speed*timer*6); 44 | return j+1; 45 | } 46 | 47 | const quickSort = (values,ids,l,r,timer) => { 48 | if(l { 56 | 57 | quickSort(values,ids,0,values.length-1,Math.ceil(Math.log(values.length+1))); 58 | 59 | setTimeout(() => { 60 | dispatch({ 61 | type:'PLAY_PAUSE', 62 | _play:false 63 | }) 64 | 65 | dispatch({ 66 | type:'UPDATE_COLOR', 67 | color: 'rgb(0, 182, 0)' 68 | }) 69 | },6*myState.speed*(1+Math.ceil(Math.log(values.length+1)))+100); 70 | } 71 | 72 | useEffect(() => { 73 | if(myState.algorithm==='quick'){ 74 | if(myState.play) 75 | solve(); 76 | } 77 | },[myState.play]); 78 | 79 | return <>; 80 | } 81 | 82 | export default QuickSort; -------------------------------------------------------------------------------- /src/components/InsertionSort.js: -------------------------------------------------------------------------------- 1 | import React,{useEffect, useState} from 'react'; 2 | import { useDispatch, useSelector } from 'react-redux'; 3 | 4 | const InsertionSort = () =>{ 5 | const myState = useSelector(state => state.updateProps); 6 | const dispatch = useDispatch(); 7 | 8 | let values = myState.values.map((item) => item[0]); 9 | let ids = myState.values.map((item) => item[1]); 10 | let timer = 0; 11 | let total_time = 0; 12 | let timing_map = new Map(); 13 | 14 | for(let i = 0; i < values.length; i++){ 15 | let j = i+1; 16 | while(j>0 && values[j] item[0]); 27 | 28 | const solve = () => { 29 | 30 | for(let i = 0; i < values.length-1; i++){ 31 | let ind = i+1; 32 | 33 | while(ind>0 && values[ind] { 48 | 49 | document.getElementById(new_ids[j]).style.transform = `translateX(${j*11}px)`; 50 | document.getElementById(new_ids[j-1]).childNodes[1].style.backgroundColor = 'black'; 51 | 52 | setTimeout(() => { 53 | document.getElementById(new_ids[j-1]).childNodes[1].style.backgroundColor = myState.color; 54 | },myState.speed-10); 55 | 56 | document.getElementById(new_ids[j-1]).style.transform = `translateX(${(j-1)*11}px)`; 57 | },timer*myState.speed); 58 | 59 | timer++; 60 | ind--; 61 | } 62 | } 63 | 64 | setTimeout(() => { 65 | dispatch({ 66 | type:'PLAY_PAUSE', 67 | _play:false 68 | }) 69 | 70 | dispatch({ 71 | type:'UPDATE_COLOR', 72 | color: 'rgb(0, 182, 0)' 73 | }) 74 | },(total_time+1)*myState.speed+50); 75 | } 76 | 77 | useEffect(() => { 78 | if(myState.algorithm==='insertion'){ 79 | if(myState.play) 80 | solve(); 81 | } 82 | },[myState.play]); 83 | 84 | return <>; 85 | } 86 | 87 | export default InsertionSort; -------------------------------------------------------------------------------- /src/components/Visuals.js: -------------------------------------------------------------------------------- 1 | import React,{useState, useEffect} from 'react'; 2 | import { useSelector, useDispatch } from 'react-redux'; 3 | import BubbleSort from './BubbleSort'; 4 | import InsertionSort from './InsertionSort'; 5 | import QuickSort from './QuickSort'; 6 | import MergeSort from './MergeSort'; 7 | import SelectionSort from './SelectionSort'; 8 | 9 | import './Visuals.css'; 10 | 11 | function Visuals() { 12 | 13 | const myState = useSelector(state => state.updateProps); 14 | const dispatch = useDispatch(); 15 | const color = myState.color; 16 | const range = myState.range; 17 | 18 | const changeValues = () => { 19 | 20 | let new_arr = [...myState.values]; 21 | for(let i = 0; i < new_arr.length; i++) 22 | document.getElementById(i).style.transform = `translateX(${i*11}px)`; 23 | 24 | dispatch({ 25 | type:'CHANGE_VALUES' 26 | }) 27 | } 28 | 29 | const handlePlayPause = (play) => { 30 | if(!myState.play){ 31 | document.getElementById('change-btn').disabled = true; 32 | document.getElementById('change-btn').style.backgroundColor = 'grey'; 33 | document.getElementById('play-btn').disabled = true; 34 | document.getElementById('play-btn').style.backgroundColor = 'grey'; 35 | } 36 | else{ 37 | return; 38 | } 39 | dispatch({ 40 | type: 'PLAY_PAUSE', 41 | _play: play 42 | }) 43 | } 44 | 45 | useEffect(() => { 46 | if(!myState.play){ 47 | document.getElementById('play-btn').disabled = false; 48 | document.getElementById('play-btn').style.backgroundColor = 'rgb(0, 149, 199)'; 49 | document.getElementById('change-btn').disabled = false; 50 | document.getElementById('change-btn').style.backgroundColor = 'rgb(0, 149, 199)'; 51 | } 52 | },[myState.play]); 53 | 54 | let speed = myState.speed; 55 | if(myState.algorithm==='selection') 56 | speed *= 3; 57 | else if(myState.algorithm==='merge') 58 | speed *= 5; 59 | else if(myState.algorithm==='quick') 60 | speed *= 6; 61 | return ( 62 |
63 |
64 | {myState.algorithm==='quick' &&
Pivot elements
} 65 | { 66 |
67 | { 68 | myState.values.map((item) => { 69 | 70 | return
71 |

{item[0]}

72 |
73 |
74 | }) 75 | } 76 |
77 | } 78 |
79 |
80 | 81 | 82 |
83 | 84 | 85 | 86 | 87 | 88 | 89 |
90 | ) 91 | } 92 | 93 | export default Visuals; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Visualize the Sorting Algos 2 | 3 | https://algovisualizer-76b0f.web.app 4 | 5 | # Getting Started with Create React App 6 | 7 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 8 | 9 | ## Available Scripts 10 | 11 | In the project directory, you can run: 12 | 13 | ### `npm start` 14 | 15 | Runs the app in the development mode.\ 16 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser. 17 | 18 | The page will reload when you make changes.\ 19 | You may also see any lint errors in the console. 20 | 21 | ### `npm test` 22 | 23 | Launches the test runner in the interactive watch mode.\ 24 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 25 | 26 | ### `npm run build` 27 | 28 | Builds the app for production to the `build` folder.\ 29 | It correctly bundles React in production mode and optimizes the build for the best performance. 30 | 31 | The build is minified and the filenames include the hashes.\ 32 | Your app is ready to be deployed! 33 | 34 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 35 | 36 | ### `npm run eject` 37 | 38 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 39 | 40 | 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. 41 | 42 | 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. 43 | 44 | 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. 45 | 46 | ## Learn More 47 | 48 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 49 | 50 | To learn React, check out the [React documentation](https://reactjs.org/). 51 | 52 | ### Code Splitting 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 55 | 56 | ### Analyzing the Bundle Size 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 59 | 60 | ### Making a Progressive Web App 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 63 | 64 | ### Advanced Configuration 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 67 | 68 | ### Deployment 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 71 | 72 | ### `npm run build` fails to minify 73 | 74 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 75 | -------------------------------------------------------------------------------- /src/components/Sidebar.js: -------------------------------------------------------------------------------- 1 | import React,{useState, useEffect} from 'react'; 2 | import { useSelector, useDispatch } from 'react-redux'; 3 | import './Sidebar.css'; 4 | import { Slider } from '@mui/material'; 5 | 6 | function Sidebar() { 7 | 8 | const myState = useSelector(state => state.updateProps); 9 | const dispatch = useDispatch(); 10 | 11 | const [max,setMax] = useState(30); 12 | 13 | const handleAlgo = (algo) => { 14 | dispatch({ 15 | type: 'UPDATE_ALGORITHM', 16 | algorithm: algo 17 | }) 18 | } 19 | 20 | const resetColor = () => { 21 | dispatch({ 22 | type:'UPDATE_COLOR', 23 | color:document.getElementById('color').value 24 | }) 25 | } 26 | 27 | const handleRange = (_range) => { 28 | 29 | let new_arr = [...myState.values]; 30 | for(let i = 0; i < new_arr.length; i++) 31 | document.getElementById(i).style.transform = `translateX(${i*11}px)`; 32 | 33 | resetColor(); 34 | 35 | dispatch({ 36 | type: 'UPDATE_RANGE', 37 | range: _range 38 | }) 39 | dispatch({ 40 | type:'CHANGE_VALUES' 41 | }) 42 | } 43 | 44 | const handleColor = (_color) => { 45 | dispatch({ 46 | type: 'UPDATE_COLOR', 47 | color: _color 48 | }) 49 | } 50 | 51 | const handleSpeed = (_speed) => { 52 | dispatch({ 53 | type: 'UPDATE_SPEED', 54 | speed: _speed 55 | }) 56 | } 57 | 58 | useEffect(() => { 59 | handleRange(30); 60 | },[]); 61 | 62 | useEffect(() => { 63 | dispatch({ 64 | type:'UPDATE_COLOR', 65 | color:document.getElementById('color').value 66 | }) 67 | },[myState.values]); 68 | 69 | const handleWidth = () => { 70 | if(window.innerWidth>1300) 71 | setMax(70); 72 | else if(window.innerWidth>1200) 73 | setMax(60); 74 | else if(window.innerWidth>1100) 75 | setMax(50); 76 | else if(window.innerWidth>900) 77 | setMax(45); 78 | else if(window.innerWidth>800) 79 | setMax(40); 80 | else if(window.innerWidth>500) 81 | setMax(30); 82 | else 83 | setMax(20); 84 | } 85 | 86 | useEffect(() => { 87 | handleWidth(); 88 | window.addEventListener('resize',handleWidth); 89 | return () => window.removeEventListener('resize',handleWidth); 90 | },[]); 91 | 92 | return ( 93 |
94 | 95 |
96 | 97 | 104 |
105 | 106 |
107 | 108 | handleRange(e.target.value)} 118 | valueLabelDisplay="auto" 119 | /> 120 |
121 | 122 |
123 | 124 | 131 |
132 | 133 |
134 | 135 | 142 |
143 |
144 | ) 145 | } 146 | 147 | export default Sidebar; --------------------------------------------------------------------------------