-
Notifications
You must be signed in to change notification settings - Fork 947
Sorting visualizer done (contributing to #749) #794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Sorting Visualizer | ||
|
|
||
| This is a project that visualizes the sorting of a given array with multiple algorithm options. | ||
| I started this project because I was always fascinated that the concept of organization could be so easily captured within algorithms, as well as the fact that some core mathematical principles can make some incredibly-fast algorithms. | ||
|  | ||
|  | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <!DOCTYPE html> | ||
|
|
||
| <html> | ||
| <head> | ||
| <title>Sorting Visualizer</title> | ||
|
|
||
| <meta charset="UTF-8" /> | ||
| <meta | ||
| name="keywords" | ||
| content="Tushar, Tushar, Projects, Sorting Visualizer, Algorithms" | ||
| /> | ||
| <meta name="description" content="Sorting Algorithm Visualizer" /> | ||
| <meta name="author" content="Tushar" /> | ||
|
|
||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
|
|
||
| <link rel="stylesheet" href="./styles.css" /> | ||
| </head> | ||
| <body> | ||
| <div class="intro"> | ||
| <h1>Sorting Algorithm Visualizer!</h1> | ||
| </div> | ||
|
|
||
| <div class="slider-container"> | ||
| <div class="slider"> | ||
| <h3>Current Array Size: <span id="sizeValue"></span></h3> | ||
| <input | ||
| type="range" | ||
| min="10" | ||
| max="100" | ||
| value="20" | ||
| class="slider" | ||
| id="sizeSlider" | ||
| /> | ||
| </div> | ||
|
|
||
| <div class="slider"> | ||
| <h3> | ||
| Current Delay (lower is faster): <span id="speedValue"></span> ms | ||
| </h3> | ||
| <input | ||
| type="range" | ||
| min="1" | ||
| max="25" | ||
| value="5" | ||
| class="slider" | ||
| id="speedSlider" | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="algo-selection-container"> | ||
| <label for="Sorting_Function">Choose an algorithm:</label> | ||
| <select id="sortingFunction" name="funcList" form="funcForm"> | ||
| <option value="bubble">Bubble Sort</option> | ||
| <option value="selection">Selection</option> | ||
| <option value="insertion">Insertion</option> | ||
| <option value="merge">Merge</option> | ||
| <option value="heap">Heap</option> | ||
| </select> | ||
| </div> | ||
|
|
||
| <div class="sorting-button-container"> | ||
| <button class="button" type="randomizeBtn" onclick="randomizeArray()"> | ||
| Generate new array | ||
| </button> | ||
| <button class="button" type="sortButton" onclick="runSort()"> | ||
| Begin Sorting | ||
| </button> | ||
| </div> | ||
|
|
||
| <div class="canvas-container"> | ||
| <canvas | ||
| id="myCanvas" | ||
| width="800" | ||
| height="400" | ||
| style="border: 1px solid #c3c3c3" | ||
| > | ||
| Your browser does not support the canvas element :,( | ||
| </canvas> | ||
| </div> | ||
| </body> | ||
| </html> | ||
|
|
||
| <script src="src/main.js"></script> | ||
| <script src="src/sort.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
|
|
||
| function clearCanvas() { | ||
| ctx.beginPath(); | ||
| ctx.fillStyle = BACKGROUND_COLOR; | ||
| ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
| ctx.closePath(); | ||
| } | ||
|
|
||
| function resetArray() { | ||
| array = []; | ||
| } | ||
|
|
||
| async function sleep() { | ||
| return new Promise((temp) => { | ||
| setTimeout(() => temp(2), SPEED); // timeout this function for duration SPEED | ||
| }); | ||
| } | ||
|
|
||
| function swap(i, j) { | ||
| let temp = array[i]; | ||
| array[i] = array[j]; | ||
| array[j] = temp; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param {index in array to draw} index | ||
| * @param {The value of the index in array} value | ||
| * @param {The new color to draw the index} color | ||
| */ | ||
| function drawBar(index, value, color = DEFAULT_COLOR) { | ||
| ctx.beginPath(); | ||
| // clear the previous bar | ||
| ctx.fillStyle = BACKGROUND_COLOR; | ||
| ctx.fillRect( | ||
| Math.ceil((WIDTH / ARR_SIZE) * index), | ||
| 0, | ||
| Math.floor(WIDTH / ARR_SIZE), | ||
| HEIGHT | ||
| ); | ||
|
|
||
| ctx.fillStyle = color; | ||
| ctx.fillRect( | ||
| Math.ceil((WIDTH / ARR_SIZE) * index), | ||
| HEIGHT, | ||
| Math.floor(WIDTH / ARR_SIZE), | ||
| Math.floor(-HEIGHT * (value / MAX_ARR_VAL)) | ||
| ); | ||
|
|
||
| ctx.closePath(); | ||
| } | ||
|
|
||
| function drawArray() { | ||
| clearCanvas(); | ||
|
|
||
| for (let i = 0; i < array.length; i++) { | ||
| let val = array[i]; | ||
| drawBar(i, val, DEFAULT_COLOR); | ||
| } | ||
| } | ||
|
|
||
| function randomizeArray() { | ||
| if (running) { | ||
| running = false; | ||
| } | ||
| clearCanvas(); | ||
| resetArray(); | ||
|
|
||
| for (let i = 0; i < ARR_SIZE; i++) { | ||
| array.push(Math.floor(Math.random() * MAX_ARR_VAL)); | ||
| } | ||
|
|
||
| drawArray(); | ||
| } | ||
|
|
||
| function runSort() { | ||
|
|
||
| if (!running) { | ||
| running = true; | ||
|
|
||
| let sortingAlgorithm; | ||
| const algoName = sortingAlgo.value; | ||
| switch (algoName) { | ||
| case "bubble": | ||
| sortingAlgorithm = bubbleSort; | ||
| break; | ||
| case "selection": | ||
| sortingAlgorithm = selectionSort; | ||
| break; | ||
| case "insertion": | ||
| sortingAlgorithm = insertionSort; | ||
| break; | ||
| case "merge": | ||
| sortingAlgorithm = mergeSort; | ||
| break; | ||
| case "quick": // TODO | ||
| sortingAlgorithm = quickSort; | ||
| break; | ||
| case "heap": | ||
| sortingAlgorithm = heapSort; | ||
| break; | ||
| default: | ||
| console.log("Invalid input"); | ||
| } | ||
|
|
||
| sortingAlgorithm(array).then((sorted) => { | ||
| if (running) { | ||
| array = sorted; | ||
| drawArray(); | ||
| running = false; | ||
| } | ||
| }); | ||
|
|
||
| } else { | ||
| console.log("Cannot restart: sorting is actively running!"); | ||
| } | ||
| } | ||
|
|
||
| function speedInput() { | ||
| SPEED = speedSlider.value; | ||
| speedOutput.innerHTML = SPEED; | ||
| } | ||
| function sizeInput() { | ||
| ARR_SIZE = sizeSlider.value; | ||
| sizeOutput.innerHTML = ARR_SIZE; | ||
| } | ||
|
|
||
| var canvas = document.getElementById("myCanvas"); | ||
| var ctx = canvas.getContext("2d"); | ||
| const WIDTH = canvas.width, | ||
| HEIGHT = canvas.height; | ||
|
|
||
| // button, slider setup | ||
| randomizeBtn = document.getElementById("randomizeBtn"); | ||
| sortingBtn = document.getElementById("sortButton"); | ||
|
|
||
| // array size | ||
| var sizeSlider = document.getElementById("sizeSlider"); | ||
| var sizeOutput = document.getElementById("sizeValue"); | ||
| var ARR_SIZE; | ||
| // getting latency | ||
| var speedSlider = document.getElementById("speedSlider"); | ||
| var speedOutput = document.getElementById("speedValue"); // get the output from the slider span | ||
| var SPEED; | ||
| // getting sorting algorithm | ||
| var sortingAlgo = document.getElementById("sortingFunction"); | ||
|
|
||
| // everytime we mess with slider, it updates values inside | ||
| speedSlider.oninput = speedInput; | ||
| sizeSlider.oninput = () => { | ||
| sizeInput(); | ||
|
|
||
| randomizeArray(); | ||
| }; | ||
|
|
||
|
|
||
| // initializing stuff I use everywhere | ||
| let array = []; | ||
| const MAX_ARR_VAL = 100; | ||
| let running = false; | ||
|
|
||
| // COLORS | ||
| const DEFAULT_COLOR = "black"; | ||
| const BACKGROUND_COLOR = "white"; | ||
|
|
||
| // first call | ||
| speedInput(); | ||
| sizeInput(); | ||
| randomizeArray(); // randomize and draw the array |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.