Skip to content

Commit 2341d51

Browse files
Merge pull request #4 from shivam7147/main
Implemented Selection Sort Visualizer
2 parents 7f21ee1 + ebf9ab8 commit 2341d51

File tree

8 files changed

+319
-8
lines changed

8 files changed

+319
-8
lines changed

package-lock.json

Lines changed: 70 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
"@tailwindcss/vite": "^4.1.14",
2020
"class-variance-authority": "^0.7.1",
2121
"clsx": "^2.1.1",
22+
"framer-motion": "^12.23.24",
2223
"lucide-react": "^0.545.0",
2324
"react": "^19.1.1",
2425
"react-dom": "^19.1.1",
26+
"react-hot-toast": "^2.6.0",
2527
"react-redux": "^9.2.0",
2628
"react-router-dom": "^7.9.4",
2729
"socket.io-client": "^4.8.1",
@@ -43,4 +45,4 @@
4345
"tw-animate-css": "^1.4.0",
4446
"vite": "^7.1.7"
4547
}
46-
}
48+
}

src/App.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import React from "react";
2-
import Homepage from "../src/pages/Homepage.jsx";
2+
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
3+
import Homepage from "./pages/Homepage";
4+
import SortingPage from "./pages/sorting/SortingPage";
35

46
function App() {
5-
return <Homepage />;
7+
return (
8+
<Router>
9+
<Routes>
10+
<Route path="/" element={<Homepage />} />
11+
<Route path="/sorting" element={<SortingPage />} />
12+
</Routes>
13+
</Router>
14+
);
615
}
716

817
export default App;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// src/algorithms/sorting/selectionSort.js
2+
export function* selectionSort(array) {
3+
const arr = [...array];
4+
const n = arr.length;
5+
6+
for (let i = 0; i < n - 1; i++) {
7+
let minIndex = i;
8+
yield { type: "compare", indices: [i] };
9+
10+
for (let j = i + 1; j < n; j++) {
11+
yield { type: "compare", indices: [minIndex, j] };
12+
13+
if (arr[j] < arr[minIndex]) {
14+
minIndex = j;
15+
yield { type: "min", index: minIndex };
16+
}
17+
}
18+
19+
if (minIndex !== i) {
20+
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
21+
yield { type: "swap", indices: [i, minIndex], array: [...arr] };
22+
}
23+
}
24+
25+
yield { type: "done", array: arr };
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from "react";
2+
3+
const COLORS = {
4+
default: "bg-blue-500 shadow-[0_0_10px_#3b82f6]",
5+
comparing: "bg-yellow-400 shadow-[0_0_12px_#facc15]",
6+
min: "bg-green-500 shadow-[0_0_12px_#22c55e]",
7+
swap: "bg-red-500 shadow-[0_0_12px_#ef4444]",
8+
};
9+
10+
export default function SelectionSortVisualizer({ array, highlight }) {
11+
const maxValue = Math.max(...array, 1); // Avoid division by zero
12+
const containerHeight = 288; // px (matches h-72)
13+
14+
return (
15+
<div className="flex items-end justify-center space-x-2 h-72 mt-10 transition-all duration-500">
16+
{array.map((value, idx) => {
17+
let color = COLORS.default;
18+
if (highlight?.type === "compare" && highlight.indices?.includes(idx))
19+
color = COLORS.comparing;
20+
if (highlight?.type === "min" && highlight.index === idx)
21+
color = COLORS.min;
22+
if (highlight?.type === "swap" && highlight.indices?.includes(idx))
23+
color = COLORS.swap;
24+
25+
// Normalize height relative to the maximum value
26+
const height = Math.max((value / maxValue) * containerHeight, 15); // minimum 15px for visibility
27+
28+
return (
29+
<div
30+
key={idx}
31+
className={`${color} w-6 transition-all duration-300 rounded-t-md`}
32+
style={{ height: `${height}px` }}
33+
></div>
34+
);
35+
})}
36+
</div>
37+
);
38+
}

src/pages/Homepage.jsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
11
import React from "react";
22
import { ArrowRight, Github } from "lucide-react";
33
import sort from "../assets/sorting.png"
4+
import { useNavigate } from "react-router-dom";
5+
46

57
const sections = [
68
{
79
title: "Sorting Algorithms",
810
description:
911
"Visualize step-by-step how sorting algorithms organize data efficiently.",
1012
phase: "Phase 1 (MVP)",
11-
img: {sort},
13+
img: "https://tamimehsan.github.io/AlgorithmVisualizer/images/sort.png?height=200&width=300",
14+
link: "/sorting",
15+
flag: false
1216
},
1317
{
1418
title: "Searching Algorithms",
1519
description:
1620
"Understand linear and binary search methods through live visualization.",
1721
phase: "Phase 1 (MVP)",
1822
img: "",
23+
link: "/searching",
24+
flag: true
1925
},
2026
{
2127
title: "Pathfinding Algorithms",
2228
description:
2329
"Watch how A*, Dijkstra and BFS explore grids to find the optimal path.",
2430
phase: "Phase 1 (MVP)",
2531
img: "",
32+
link: "/pathfinding",
33+
flag: true
2634
},
2735
{
2836
title: "Graph Algorithms",
2937
description:
3038
"Explore BFS, DFS, Kruskal’s, Prim’s, and more — all brought to life interactively.",
3139
phase: "Phase 2",
3240
img: "",
41+
link: "/graphs",
42+
flag: true
3343
},
3444
{
3545
title: "Recursion & Backtracking",
3646
description:
3747
"Visualize recursive calls and backtracking patterns like N-Queens or Sudoku.",
3848
phase: "Phase 2",
3949
img: "",
50+
link: "/recursion",
51+
flag: true
4052
},
4153
{
4254
title: "Data Structures Visualization",
4355
description:
4456
"Interactively understand stacks, queues, linked lists, trees, and heaps.",
4557
phase: "Phase 2",
4658
img: "",
59+
link: "/data-structures",
60+
flag: true
4761
},
4862
{
4963
title: "Dynamic Programming",
5064
description:
5165
"Step through state transitions and table updates to grasp DP intuitively.",
5266
phase: "Phase 3",
5367
img: "",
68+
link: "/dynamic-programming",
69+
flag: true
5470
},
5571
];
5672

5773

5874
const Homepage = () => {
75+
const navigate = useNavigate();
76+
5977
return (
6078
<div className="min-h-screen w-full text-white flex flex-col items-center overflow-x-hidden relative">
6179
{/* Full-Page Animated Gradient Background */}
@@ -93,6 +111,7 @@ const Homepage = () => {
93111
{sections.map((section, index) => (
94112
<div
95113
key={index}
114+
onClick={() => section.link && navigate(section.link)}
96115
className="relative group bg-white/10 border border-white/10 rounded-2xl overflow-hidden shadow-2xl hover:shadow-indigo-500/40 transition-all duration-300 backdrop-blur-md hover:-translate-y-2"
97116
>
98117
{/* Image */}
@@ -119,9 +138,11 @@ const Homepage = () => {
119138
</div>
120139

121140
{/* SaaS-style “Coming Soon” Overlay */}
122-
<div className="absolute inset-0 flex items-center justify-center text-white text-lg font-semibold bg-gradient-to-br from-indigo-700/70 to-purple-900/70 backdrop-blur-md opacity-0 group-hover:opacity-100 transition-opacity duration-300">
123-
Coming Soon 🚀
124-
</div>
141+
{section.flag && (
142+
<div className="absolute inset-0 flex items-center justify-center text-white text-lg font-semibold bg-gradient-to-br from-indigo-700/70 to-purple-900/70 backdrop-blur-md opacity-0 group-hover:opacity-100 transition-opacity duration-300">
143+
Coming Soon 🚀
144+
</div>
145+
)}
125146
</div>
126147
))}
127148
</section>

0 commit comments

Comments
 (0)