Skip to content

Commit 20ab2a0

Browse files
SortingVisualizer
1 parent 8e49ce9 commit 20ab2a0

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
4+
<link rel = "stylesheet" href = "style.css">
5+
</head>
6+
<body>
7+
<div id="canvas">
8+
9+
</div>
10+
<div class="buttons">
11+
<button onclick="sort()">Sort</button>
12+
<button onclick="init()">Randomize</button>
13+
</div>
14+
<script src="script.js"></script>
15+
</body>
16+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Sorting-Visualizer
2+
The Sorting Visualizer is a basic web application that allows you to visualize the process of sorting on a set of randomly generated bars.
3+
4+
# Technologies Used
5+
6+
HTML
7+
8+
CSS
9+
10+
JavaScript
11+
12+
# How to Use
13+
Clone this repository to your local machine.
14+
15+
Open index.html in your web browser.
16+
17+
Click the "Randomize" button to generate a random set of bars.
18+
19+
Select a sorting algorithm from the dropdown menu and click the "Sort" button to visualize the sorting process.
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const canvas = document.getElementById("canvas");
2+
3+
let n = 20;
4+
let arr = [];
5+
function init(){
6+
for(let i=0;i<n;i++){
7+
arr[i] = Math.random();
8+
}
9+
LineDraw(-1,"cyan");
10+
}
11+
function LineDraw(index, color){
12+
canvas.innerHTML = "";
13+
for(let i = 0; i< arr.length; i++){
14+
let bar = document.createElement("div");
15+
bar.style.height = arr[i]*100 + "%";
16+
bar.classList.add("bar");
17+
canvas.appendChild(bar);
18+
if(index != -1 && (index === i || index === i-1)){
19+
bar.style.backgroundColor = color;
20+
}
21+
else {
22+
bar.style.backgroundColor= "#03a9f4";
23+
}
24+
}
25+
}
26+
27+
init();
28+
async function checker(i){
29+
if(arr[i]>arr[i+1]){
30+
[arr[i],arr[i+1]] = [arr[i+1], arr[i]];
31+
LineDraw(i,"purple");
32+
}
33+
else{
34+
LineDraw(i,"purple");
35+
}
36+
}
37+
async function BubbleSort(n){
38+
if(n===1){
39+
return;
40+
}
41+
42+
for(let i = 0; i< arr.length ; i++){
43+
setTimeout(async ()=>{
44+
const result = await checker(i);
45+
},300);
46+
}
47+
48+
BubbleSort(n-1);
49+
50+
}
51+
function sort(){
52+
BubbleSort(arr.length);
53+
LineDraw(-1,"cyan");
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.bar {
2+
width: 10px;
3+
background-color: #03a9f4;
4+
margin: 1px;
5+
transition: height 0.2s;
6+
}
7+
8+
9+
#canvas {
10+
display: flex;
11+
justify-content: center;
12+
align-items: flex-end;
13+
flex-direction: row;
14+
height: 50%;
15+
width: 50%;
16+
background: rgb(238, 174, 202);
17+
background: radial-gradient(circle, rgb(255, 217, 234) 0%, rgba(148, 187, 233, 1) 100%);
18+
padding: 10px;
19+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
20+
}
21+
22+
23+
body {
24+
display: flex;
25+
align-items: center;
26+
justify-content: center;
27+
background: snow;
28+
margin: 0;
29+
padding: 0;
30+
height: 100vh;
31+
font-family: Arial, sans-serif;
32+
}
33+
34+
.buttons {
35+
display: flex;
36+
flex-direction: row;
37+
position: absolute;
38+
top : 80%;
39+
}
40+
41+
button {
42+
padding: 10px 20px;
43+
font-size: 16px;
44+
border: none;
45+
border-radius: 5px;
46+
cursor: pointer;
47+
margin: 10px;
48+
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
49+
}
50+

0 commit comments

Comments
 (0)