Skip to content

Commit b451cb9

Browse files
committed
Added Searching Algorithms
1 parent 241f127 commit b451cb9

File tree

10 files changed

+578
-7
lines changed

10 files changed

+578
-7
lines changed

public/searching.png

7.34 KB
Loading

src/App.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import GraphPage from "./pages/graph/GraphPage";
66
import Homepage from "./pages/Homepage.jsx";
77
import DSPage from "./pages/dataStructure/datastructurePage.jsx"
88
import DynamicProgrammingPage from "./pages/dynamic-programming/DyanmicProgrammingPage.jsx";
9-
9+
import Searchingpage from "./pages/searching/searchingPage";
1010
function App() {
1111
return (
1212
<Router>
1313
<Routes>
1414
<Route path="/" element={<Homepage />} />
1515
{/* <Route path="/graph/union-find" element={<UnionFindPage />} /> */}
1616
<Route path="/sorting" element={<SortingPage />} />
17+
<Route path="/searching" element={<Searchingpage />} />
1718
<Route path="/data-structures" element={<DSPage/>}/>
1819
<Route path="/graph" element={<GraphPage />} />
1920
<Route path="/dynamic-programming" element={<DynamicProgrammingPage />} />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* A generator function that performs Binary Search and yields visualization steps.
3+
* @param {number[]} array - The array to search through (must be sorted).
4+
* @param {number} target - The value to find.
5+
* @yields {{lowIndex: number, highIndex: number, midIndex: number | null, foundIndex: number | null, message?: string}}
6+
*/
7+
export function* binarySearch(array, target) {
8+
const targetNumber = parseInt(target);
9+
let low = 0;
10+
let high = array.length - 1;
11+
let foundIndex = null;
12+
13+
// Initial state: show the full search range
14+
yield { lowIndex: low, highIndex: high, midIndex: null, foundIndex: null };
15+
16+
while (low <= high) {
17+
let mid = Math.floor((low + high) / 2);
18+
19+
// Step 1: Highlight the middle element being compared (yellow)
20+
yield {
21+
lowIndex: low,
22+
highIndex: high,
23+
midIndex: mid,
24+
foundIndex: null
25+
};
26+
27+
// Step 2: Check for the target
28+
if (array[mid] === targetNumber) {
29+
foundIndex = mid;
30+
// Target found, yield the final state (green) and stop
31+
yield {
32+
lowIndex: low,
33+
highIndex: high,
34+
midIndex: mid,
35+
foundIndex: foundIndex,
36+
message: `Success! Found target ${targetNumber} at index ${mid}`
37+
};
38+
return; // Exit the generator
39+
} else if (array[mid] < targetNumber) {
40+
// Target is greater, search the right half. Discard the lower half by moving low.
41+
low = mid + 1;
42+
} else {
43+
// Target is smaller, search the left half. Discard the upper half by moving high.
44+
high = mid - 1;
45+
}
46+
47+
// Step 3: Yield the updated boundaries for visualization before the next comparison
48+
yield {
49+
lowIndex: low,
50+
highIndex: high,
51+
midIndex: null, // Clear the mid highlight
52+
foundIndex: null
53+
};
54+
}
55+
56+
// If the loop finishes without finding the target
57+
yield {
58+
lowIndex: low,
59+
highIndex: high,
60+
midIndex: null,
61+
foundIndex: null,
62+
message: `Target ${targetNumber} was not found in the array.`
63+
};
64+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* A generator function that performs Linear Search and yields visualization steps.
3+
* @param {number[]} array - The array to search through.
4+
* @param {number} target - The value to find.
5+
* @yields {{highlightIndex: number | null, foundIndex: number | null, message?: string}}
6+
*/
7+
export function* linearSearch(array, target) {
8+
const targetNumber = parseInt(target); // Ensure target is a number
9+
let foundIndex = null;
10+
11+
// Iterate through the array
12+
for (let i = 0; i < array.length; i++) {
13+
// 1. Yield the current index being compared (yellow highlight)
14+
yield {
15+
highlightIndex: i,
16+
foundIndex: foundIndex
17+
};
18+
19+
// 2. Check for the target
20+
if (array[i] === targetNumber) {
21+
foundIndex = i;
22+
// Target found, yield the final state (green) and return
23+
yield {
24+
highlightIndex: i,
25+
foundIndex: foundIndex,
26+
message: `Success! Found target ${targetNumber} at index ${i}`
27+
};
28+
return; // Stop the generator
29+
}
30+
}
31+
32+
// If the loop finishes without finding the target
33+
yield {
34+
highlightIndex: null, // Clear highlight
35+
foundIndex: null, // Not found
36+
message: `Target ${targetNumber} was not found in the array.`
37+
};
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from "react";
2+
3+
const COLORS = {
4+
default: "bg-blue-800", // Bar outside the search range (eliminated)
5+
activeRange: "bg-blue-500 shadow-[0_0_10px_#3b82f6]", // Bar inside the current search range
6+
comparing: "bg-yellow-400 shadow-[0_0_12px_#facc15]", // Element currently being checked (mid)
7+
found: "bg-green-500 shadow-[0_0_12px_#22c55e]", // Target element found
8+
};
9+
10+
export default function BinarySearchVisualizer({
11+
array,
12+
lowIndex,
13+
highIndex,
14+
midIndex,
15+
foundIndex
16+
}) {
17+
const maxValue = Math.max(...array, 1);
18+
const containerHeight = 288; // px (matches h-72)
19+
20+
return (
21+
<div className="flex items-end justify-center space-x-2 h-72 mt-10 transition-all duration-500">
22+
{array.map((value, idx) => {
23+
let color = COLORS.default;
24+
25+
// 1. Check if the element is inside the current search range
26+
if (idx >= lowIndex && idx <= highIndex) {
27+
color = COLORS.activeRange;
28+
}
29+
30+
// 2. Check if the element is currently being compared (mid)
31+
if (idx === midIndex) {
32+
color = COLORS.comparing;
33+
}
34+
35+
// 3. Check if the element has been found (Highest priority)
36+
if (idx === foundIndex) {
37+
color = COLORS.found;
38+
}
39+
40+
// Normalize height relative to the maximum value
41+
const height = Math.max((value / maxValue) * containerHeight, 15);
42+
43+
return (
44+
<div
45+
key={idx}
46+
className={`${color} w-6 transition-all duration-300 rounded-t-md`}
47+
style={{ height: `${height}px` }}
48+
></div>
49+
);
50+
})}
51+
</div>
52+
);
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from "react";
2+
3+
// Updated colors for searching visualization
4+
const COLORS = {
5+
default: "bg-blue-500 shadow-[0_0_10px_#3b82f6]", // Default bar color
6+
comparing: "bg-yellow-400 shadow-[0_0_12px_#facc15]", // Element currently being checked
7+
found: "bg-green-500 shadow-[0_0_12px_#22c55e]", // Target element found
8+
miss: "bg-red-500 shadow-[0_0_12px_#ef4444]", // Optional: To highlight elements that were skipped (e.g., in Binary Search)
9+
};
10+
11+
export default function LinearSearchVisualizer({
12+
array,
13+
highlightIndex,
14+
foundIndex
15+
}) {
16+
const maxValue = Math.max(...array, 1);
17+
const containerHeight = 288; // px (matches h-72)
18+
19+
return (
20+
<div className="flex items-end justify-center space-x-2 h-72 mt-10 transition-all duration-500">
21+
{array.map((value, idx) => {
22+
let color = COLORS.default;
23+
24+
// 1. Check if the element has been found (Highest priority for the final result)
25+
if (foundIndex === idx) {
26+
color = COLORS.found;
27+
}
28+
// 2. Check if the element is currently being compared
29+
else if (highlightIndex === idx) {
30+
color = COLORS.comparing;
31+
}
32+
33+
// Normalize height relative to the maximum value
34+
const height = Math.max((value / maxValue) * containerHeight, 15);
35+
36+
return (
37+
<div
38+
key={idx}
39+
className={`${color} w-6 transition-all duration-300 rounded-t-md`}
40+
style={{ height: `${height}px` }}
41+
></div>
42+
);
43+
})}
44+
</div>
45+
);
46+
}

src/pages/Homepage.jsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ArrowRight, Github } from "lucide-react";
33
import sort from "../assets/sorting.png"
44
import { useNavigate } from "react-router-dom";
55

6+
67
const sections = [
78
{
89
title: "Sorting Algorithms",
@@ -18,9 +19,9 @@ const sections = [
1819
description:
1920
"Understand linear and binary search methods through live visualization.",
2021
phase: "Phase 1 (MVP)",
21-
img: "",
22+
img: "/searching.png",
2223
link: "/searching",
23-
flag: true
24+
flag: false
2425
},
2526
{
2627
title: "Pathfinding Algorithms",
@@ -116,7 +117,7 @@ const Homepage = () => {
116117
<div
117118
key={index}
118119
onClick={() => section.link && navigate(section.link)}
119-
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"
120+
className="relative group bg-white/10 border border-white/10 rounded-2xl overflow-hidden shadow-2xl hover:shadow-indigo-500/40 cursor-pointer transition-all duration-300 backdrop-blur-md hover:-translate-y-2"
120121
>
121122
{/* Image */}
122123
<img
@@ -138,11 +139,11 @@ const Homepage = () => {
138139
{/* Explore Button */}
139140
<button
140141
onClick={() =>
141-
section.route
142-
? navigate(section.route)
142+
section.link
143+
? navigate(section.link)
143144
: alert("Coming soon 🚀")
144145
}
145-
className="mt-4 text-indigo-400 font-semibold flex items-center gap-1 hover:gap-2 transition-all"
146+
className="mt-4 text-indigo-400 font-semibold flex items-center gap-1 hover:gap-2 cursor-pointer transition-all"
146147
>
147148
Explore <ArrowRight size={16} />
148149
</button>

0 commit comments

Comments
 (0)