|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { Toaster } from "react-hot-toast"; |
| 3 | +import InsertionSortVisualizer from "../../components/sorting/InsertionSortVisualizer"; |
| 4 | +import { insertionSort } from "../../algorithms/sorting/InsertionSort"; |
| 5 | + |
| 6 | +export default function InsertionSort() { |
| 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 = insertionSort(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 | + Insertion 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 | + <InsertionSortVisualizer array={array} highlight={highlight} /> |
| 79 | + </div> |
| 80 | + |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
0 commit comments