An interactive binary search algorithm visualizer with step-by-step animation. Watch the L/Mid/R pointers move, elements eliminated in real time, confetti on success, and a live step counter. Built with pure HTML, CSS, and vanilla JavaScript.
Every step in the binary search algorithm with code reference.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Binary Search Visualizer</title> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700;900&display=swap" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script> <link rel="stylesheet" href="style.css" /> </head> <body> <!-- Dark mode toggle --> <button class="toggle" onclick="document.body.classList.toggle('dark')">Dark Mode</button> <h1>Binary Search</h1> <p class="subtitle">O(log n) • Requires sorted array • Halving the search space</p> <div id="visualizer"> <div class="pointers" id="pointers"> <div class="pointer" id="left">L</div> <div class="pointer" id="mid">MID</div> <div class="pointer" id="right">R</div> </div> <div class="array" id="array"></div> </div> <div class="controls"> <input type="number" id="target" placeholder="Target value" value="42" /> <div> <button class="search" onclick="startBinarySearch()">START BINARY SEARCH</button> <button class="random" onclick="generateSortedArray()">New Sorted Array</button> <button class="reset" onclick="reset()">Reset</button> </div> </div> <div class="stats"> <div>Steps: <span id="steps">0</span></div> <div>Comparisons: <span id="comparisons">0</span></div> <div>Array Size: <span id="size">21</span></div> </div> <div class="status" id="status">Click "New Sorted Array" then enter a target and START!</div> <script src="script.js"></script> </body> </html>
How binary search works and how this visualizer teaches it.
Binary search works on a sorted array by halving the search space each step. It sets left = 0 and right = array.length - 1, calculates mid = Math.floor((left + right) / 2), then compares array[mid] to the target. If equal: found. If array[mid] is less than target: left = mid + 1. If array[mid] is greater: right = mid - 1. This repeats while left <= right. Maximum steps: ceil(log₂(n)) — just 5 steps for 21 elements.
Each iteration calls render() to redraw all cells, then adds the .mid CSS class to the middle cell (triggering a scale(1.5) + 360° spin amber animation). The L, MID, and R pointer labels are repositioned using absolute left: calc(index * 126px). Eliminated elements get the .eliminated class (scale(0.7), grey, opacity 0.6). An async delay() of 1.2 seconds pauses between steps via setTimeout in a Promise. When found, .found class fires a scale(1.8) green glow and canvas-confetti.
O(log n) means the maximum number of steps grows logarithmically with the array size. For 21 elements: at most 5 steps. For 1,000 elements: at most 10 steps. For 1,000,000 elements: at most 20 steps. This is because each step eliminates exactly half the remaining elements. Binary search is exponentially faster than linear search O(n) for large datasets, but requires a pre-sorted array.
The toggle button calls document.body.classList.toggle('dark'). The CSS has two sets of custom property values — :root defines the light mode variables (green background, white card), and .dark overrides them (dark forest background, transparent green card). Since all element styles reference var(--bg), var(--card), var(--text), and var(--cell), toggling the class instantly switches all colors via CSS cascade — no JavaScript required beyond the one classList call.
Yes. The Binary Search Visualizer is one of the best ways to intuitively understand binary search before a technical interview. It shows exactly how L/Mid/R pointers move, why the sorted array requirement exists, how the elimination process halves the search space, and the relationship between array size and maximum steps (O(log n)). Pair it with the free Coodeverse DSA course for comprehensive interview prep.
Yes. Click Download Project above to get the full HTML, CSS, and JavaScript as a ZIP file — free, no account needed. Coodeverse provides 540+ free downloadable coding projects. The code uses async/await, CSS custom properties, canvas-confetti, and vanilla DOM manipulation — all great patterns to study for frontend development.
Free courses in DSA, JavaScript, Python, SQL, Kotlin, HTML, CSS. No account needed.