Skip to content

Commit 284beaf

Browse files
committed
resolved the issue
1 parent 7f21ee1 commit 284beaf

File tree

8 files changed

+303
-5
lines changed

8 files changed

+303
-5
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
return (
12+
<div className="flex items-end justify-center space-x-2 h-72 mt-10 transition-all duration-500">
13+
{array.map((value, idx) => {
14+
let color = COLORS.default;
15+
if (highlight?.type === "compare" && highlight.indices?.includes(idx))
16+
color = COLORS.comparing;
17+
if (highlight?.type === "min" && highlight.index === idx)
18+
color = COLORS.min;
19+
if (highlight?.type === "swap" && highlight.indices?.includes(idx))
20+
color = COLORS.swap;
21+
22+
const height = Math.max(value * 5, 15); // Ensures visibility for small numbers
23+
24+
return (
25+
<div
26+
key={idx}
27+
className={`${color} w-6 transition-all duration-300 rounded-t-md`}
28+
style={{ height: `${height}px` }}
29+
></div>
30+
);
31+
})}
32+
</div>
33+
);
34+
}

src/pages/Homepage.jsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,72 @@
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",
1215
},
1316
{
1417
title: "Searching Algorithms",
1518
description:
1619
"Understand linear and binary search methods through live visualization.",
1720
phase: "Phase 1 (MVP)",
1821
img: "",
22+
link: "/searching"
1923
},
2024
{
2125
title: "Pathfinding Algorithms",
2226
description:
2327
"Watch how A*, Dijkstra and BFS explore grids to find the optimal path.",
2428
phase: "Phase 1 (MVP)",
2529
img: "",
30+
link: "/pathfinding"
2631
},
2732
{
2833
title: "Graph Algorithms",
2934
description:
3035
"Explore BFS, DFS, Kruskal’s, Prim’s, and more — all brought to life interactively.",
3136
phase: "Phase 2",
3237
img: "",
38+
link: "/graphs"
3339
},
3440
{
3541
title: "Recursion & Backtracking",
3642
description:
3743
"Visualize recursive calls and backtracking patterns like N-Queens or Sudoku.",
3844
phase: "Phase 2",
3945
img: "",
46+
link: "/recursion"
4047
},
4148
{
4249
title: "Data Structures Visualization",
4350
description:
4451
"Interactively understand stacks, queues, linked lists, trees, and heaps.",
4552
phase: "Phase 2",
4653
img: "",
54+
link: "/data-structures"
4755
},
4856
{
4957
title: "Dynamic Programming",
5058
description:
5159
"Step through state transitions and table updates to grasp DP intuitively.",
5260
phase: "Phase 3",
5361
img: "",
62+
link: "/dynamic-programming"
5463
},
5564
];
5665

5766

5867
const Homepage = () => {
68+
const navigate = useNavigate();
69+
5970
return (
6071
<div className="min-h-screen w-full text-white flex flex-col items-center overflow-x-hidden relative">
6172
{/* Full-Page Animated Gradient Background */}
@@ -93,6 +104,7 @@ const Homepage = () => {
93104
{sections.map((section, index) => (
94105
<div
95106
key={index}
107+
onClick={() => section.link && navigate(section.link)}
96108
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"
97109
>
98110
{/* Image */}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React, { useState } from "react";
2+
import { Toaster } from "react-hot-toast";
3+
import SelectionSortVisualizer from "../../components/sorting/SelectionSortVisualizer";
4+
import { selectionSort } from "../../algorithms/sorting/selectionSort";
5+
6+
export default function SelectionSort() {
7+
const [array, setArray] = useState([]);
8+
const [input, setInput] = useState("");
9+
const [highlight, setHighlight] = useState(null);
10+
const [isRunning, setIsRunning] = useState(false);
11+
12+
const handleStart = async () => {
13+
if (isRunning || array.length === 0) return;
14+
setIsRunning(true);
15+
16+
const gen = selectionSort(array);
17+
for (let step of gen) {
18+
setHighlight(step);
19+
if (step.array) setArray([...step.array]);
20+
await new Promise((r) => setTimeout(r, 500));
21+
}
22+
23+
setHighlight({ type: "done" });
24+
setIsRunning(false);
25+
};
26+
27+
const handleReset = () => {
28+
setArray([]);
29+
setInput("");
30+
setHighlight(null);
31+
};
32+
33+
const handleInput = (e) => {
34+
setInput(e.target.value);
35+
const numbers = e.target.value
36+
.split(",")
37+
.map((n) => parseInt(n.trim()))
38+
.filter((n) => !isNaN(n));
39+
setArray(numbers);
40+
};
41+
42+
return (
43+
<div className="min-h-screen bg-black text-gray-200 flex flex-col items-center p-6">
44+
<Toaster position="top-center" />
45+
<h1 className="text-4xl font-extrabold mb-8 text-indigo-400 drop-shadow-lg">
46+
Selection Sort Visualizer
47+
</h1>
48+
49+
<input
50+
type="text"
51+
value={input}
52+
onChange={handleInput}
53+
placeholder="Enter numbers separated by commas"
54+
className="border-2 border-indigo-500 bg-gray-900 text-indigo-200 rounded-lg p-3 w-96 text-center shadow-lg focus:ring-2 focus:ring-indigo-400 outline-none"
55+
/>
56+
57+
<div className="space-x-4 mt-6">
58+
<button
59+
onClick={handleStart}
60+
disabled={isRunning}
61+
className={`${
62+
isRunning
63+
? "bg-indigo-700 text-gray-300 cursor-not-allowed"
64+
: "bg-indigo-600 hover:bg-indigo-500"
65+
} px-6 py-2 rounded-lg text-white font-semibold shadow-md transition-all duration-300`}
66+
>
67+
{isRunning ? "Sorting..." : "Start Visualization"}
68+
</button>
69+
<button
70+
onClick={handleReset}
71+
className="bg-gray-700 hover:bg-gray-600 px-6 py-2 rounded-lg text-white font-semibold shadow-md transition-all duration-300"
72+
>
73+
Reset
74+
</button>
75+
</div>
76+
77+
<div className="mt-15">
78+
<SelectionSortVisualizer array={array} highlight={highlight} />
79+
</div>
80+
81+
</div>
82+
);
83+
}

0 commit comments

Comments
 (0)