Skip to content

Commit 3b2d38d

Browse files
authored
Merge pull request #794 from Tushar2930/master
Sorting visualizer done (contributing to #749)
2 parents ac6c572 + b279010 commit 3b2d38d

File tree

5 files changed

+588
-0
lines changed

5 files changed

+588
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Sorting Visualizer
2+
3+
This is a project that visualizes the sorting of a given array with multiple algorithm options.
4+
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.
5+
![111](https://github.com/Tushar2930/javascript-mini-projects/assets/110845672/23f8cda7-aa40-4d07-9ed9-7fbd553181ab)
6+
![222](https://github.com/Tushar2930/javascript-mini-projects/assets/110845672/d93fe7cb-83f6-4ea9-bd8c-3e7d69e6a307)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<title>Sorting Visualizer</title>
6+
7+
<meta charset="UTF-8" />
8+
<meta
9+
name="keywords"
10+
content="Tushar, Tushar, Projects, Sorting Visualizer, Algorithms"
11+
/>
12+
<meta name="description" content="Sorting Algorithm Visualizer" />
13+
<meta name="author" content="Tushar" />
14+
15+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
16+
17+
<link rel="stylesheet" href="./styles.css" />
18+
</head>
19+
<body>
20+
<div class="intro">
21+
<h1>Sorting Algorithm Visualizer!</h1>
22+
</div>
23+
24+
<div class="slider-container">
25+
<div class="slider">
26+
<h3>Current Array Size: <span id="sizeValue"></span></h3>
27+
<input
28+
type="range"
29+
min="10"
30+
max="100"
31+
value="20"
32+
class="slider"
33+
id="sizeSlider"
34+
/>
35+
</div>
36+
37+
<div class="slider">
38+
<h3>
39+
Current Delay (lower is faster): <span id="speedValue"></span> ms
40+
</h3>
41+
<input
42+
type="range"
43+
min="1"
44+
max="25"
45+
value="5"
46+
class="slider"
47+
id="speedSlider"
48+
/>
49+
</div>
50+
</div>
51+
52+
<div class="algo-selection-container">
53+
<label for="Sorting_Function">Choose an algorithm:</label>
54+
<select id="sortingFunction" name="funcList" form="funcForm">
55+
<option value="bubble">Bubble Sort</option>
56+
<option value="selection">Selection</option>
57+
<option value="insertion">Insertion</option>
58+
<option value="merge">Merge</option>
59+
<option value="heap">Heap</option>
60+
</select>
61+
</div>
62+
63+
<div class="sorting-button-container">
64+
<button class="button" type="randomizeBtn" onclick="randomizeArray()">
65+
Generate new array
66+
</button>
67+
<button class="button" type="sortButton" onclick="runSort()">
68+
Begin Sorting
69+
</button>
70+
</div>
71+
72+
<div class="canvas-container">
73+
<canvas
74+
id="myCanvas"
75+
width="800"
76+
height="400"
77+
style="border: 1px solid #c3c3c3"
78+
>
79+
Your browser does not support the canvas element :,(
80+
</canvas>
81+
</div>
82+
</body>
83+
</html>
84+
85+
<script src="src/main.js"></script>
86+
<script src="src/sort.js"></script>
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
2+
function clearCanvas() {
3+
ctx.beginPath();
4+
ctx.fillStyle = BACKGROUND_COLOR;
5+
ctx.fillRect(0, 0, canvas.width, canvas.height);
6+
ctx.closePath();
7+
}
8+
9+
function resetArray() {
10+
array = [];
11+
}
12+
13+
async function sleep() {
14+
return new Promise((temp) => {
15+
setTimeout(() => temp(2), SPEED); // timeout this function for duration SPEED
16+
});
17+
}
18+
19+
function swap(i, j) {
20+
let temp = array[i];
21+
array[i] = array[j];
22+
array[j] = temp;
23+
}
24+
25+
/**
26+
*
27+
* @param {index in array to draw} index
28+
* @param {The value of the index in array} value
29+
* @param {The new color to draw the index} color
30+
*/
31+
function drawBar(index, value, color = DEFAULT_COLOR) {
32+
ctx.beginPath();
33+
// clear the previous bar
34+
ctx.fillStyle = BACKGROUND_COLOR;
35+
ctx.fillRect(
36+
Math.ceil((WIDTH / ARR_SIZE) * index),
37+
0,
38+
Math.floor(WIDTH / ARR_SIZE),
39+
HEIGHT
40+
);
41+
42+
ctx.fillStyle = color;
43+
ctx.fillRect(
44+
Math.ceil((WIDTH / ARR_SIZE) * index),
45+
HEIGHT,
46+
Math.floor(WIDTH / ARR_SIZE),
47+
Math.floor(-HEIGHT * (value / MAX_ARR_VAL))
48+
);
49+
50+
ctx.closePath();
51+
}
52+
53+
function drawArray() {
54+
clearCanvas();
55+
56+
for (let i = 0; i < array.length; i++) {
57+
let val = array[i];
58+
drawBar(i, val, DEFAULT_COLOR);
59+
}
60+
}
61+
62+
function randomizeArray() {
63+
if (running) {
64+
running = false;
65+
}
66+
clearCanvas();
67+
resetArray();
68+
69+
for (let i = 0; i < ARR_SIZE; i++) {
70+
array.push(Math.floor(Math.random() * MAX_ARR_VAL));
71+
}
72+
73+
drawArray();
74+
}
75+
76+
function runSort() {
77+
78+
if (!running) {
79+
running = true;
80+
81+
let sortingAlgorithm;
82+
const algoName = sortingAlgo.value;
83+
switch (algoName) {
84+
case "bubble":
85+
sortingAlgorithm = bubbleSort;
86+
break;
87+
case "selection":
88+
sortingAlgorithm = selectionSort;
89+
break;
90+
case "insertion":
91+
sortingAlgorithm = insertionSort;
92+
break;
93+
case "merge":
94+
sortingAlgorithm = mergeSort;
95+
break;
96+
case "quick": // TODO
97+
sortingAlgorithm = quickSort;
98+
break;
99+
case "heap":
100+
sortingAlgorithm = heapSort;
101+
break;
102+
default:
103+
console.log("Invalid input");
104+
}
105+
106+
sortingAlgorithm(array).then((sorted) => {
107+
if (running) {
108+
array = sorted;
109+
drawArray();
110+
running = false;
111+
}
112+
});
113+
114+
} else {
115+
console.log("Cannot restart: sorting is actively running!");
116+
}
117+
}
118+
119+
function speedInput() {
120+
SPEED = speedSlider.value;
121+
speedOutput.innerHTML = SPEED;
122+
}
123+
function sizeInput() {
124+
ARR_SIZE = sizeSlider.value;
125+
sizeOutput.innerHTML = ARR_SIZE;
126+
}
127+
128+
var canvas = document.getElementById("myCanvas");
129+
var ctx = canvas.getContext("2d");
130+
const WIDTH = canvas.width,
131+
HEIGHT = canvas.height;
132+
133+
// button, slider setup
134+
randomizeBtn = document.getElementById("randomizeBtn");
135+
sortingBtn = document.getElementById("sortButton");
136+
137+
// array size
138+
var sizeSlider = document.getElementById("sizeSlider");
139+
var sizeOutput = document.getElementById("sizeValue");
140+
var ARR_SIZE;
141+
// getting latency
142+
var speedSlider = document.getElementById("speedSlider");
143+
var speedOutput = document.getElementById("speedValue"); // get the output from the slider span
144+
var SPEED;
145+
// getting sorting algorithm
146+
var sortingAlgo = document.getElementById("sortingFunction");
147+
148+
// everytime we mess with slider, it updates values inside
149+
speedSlider.oninput = speedInput;
150+
sizeSlider.oninput = () => {
151+
sizeInput();
152+
153+
randomizeArray();
154+
};
155+
156+
157+
// initializing stuff I use everywhere
158+
let array = [];
159+
const MAX_ARR_VAL = 100;
160+
let running = false;
161+
162+
// COLORS
163+
const DEFAULT_COLOR = "black";
164+
const BACKGROUND_COLOR = "white";
165+
166+
// first call
167+
speedInput();
168+
sizeInput();
169+
randomizeArray(); // randomize and draw the array

0 commit comments

Comments
 (0)