Binary Search Visualizer by Coodeverse is a free interactive binary search algorithm visualizer built with pure HTML, CSS, and vanilla JavaScript. Enter a target number and watch the algorithm search step by step — animated L (left), Mid, and R (right) pointers move each iteration, eliminated elements fade out, and confetti fires on success. Dark/light mode toggle, real-time step and comparison counters. O(log n) time complexity visualized intuitively. Full source code free at coodeverse.com/projects/binary-search-visualizer.
🔍Algorithm:Binary Search
·
⚡Complexity:O(log n)
·
🎯Pointers:L · Mid · R
·
🌗Modes:Dark + Light
·
🎉Success FX:400-particle confetti
Live Preview

🔍 Binary Search — Algorithm Steps

Every step in the binary search algorithm with code reference.

Step 1
Initialize Pointers
Set left = 0 and right = array.length - 1. These define the current search range — the entire array at start.
let left = 0, right = array.length - 1;
Step 2
Calculate Mid
Calculate the middle index as Math.floor((left + right) / 2). This is the element checked this iteration.
const mid = Math.floor((left + right) / 2);
Step 3a
Found — Target Match
If array[mid] === target, the element is found. The mid cell scales up, turns green, confetti fires, and search ends.
if (array[mid] === target) → found!
Step 3b
Too Small — Go Right
If array[mid] < target, the target must be in the right half. Eliminate elements from left to mid. Set left = mid + 1.
if (array[mid] < target) left = mid + 1;
Step 3c
Too Big — Go Left
If array[mid] > target, the target must be in the left half. Eliminate elements from mid to right. Set right = mid - 1.
if (array[mid] > target) right = mid - 1;
Step 4
Repeat or Not Found
Loop while left <= right. Each iteration halves the search space. When left > right, the target is not in the array — O(log n) steps maximum.
while (left <= right) { ... }
🔍 Full Source Code
// index.html — Binary Search Visualizer
<!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>
// binary_search_faq

Binary Search — FAQ

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.

Master DSA & Algorithms.
Build Real Projects.

Free courses in DSA, JavaScript, Python, SQL, Kotlin, HTML, CSS. No account needed.

📊 DSA 🟡 JavaScript 🐍 Python 🌐 HTML 🎨 CSS 🗄️ SQL 🟣 Kotlin
Learn DSA Free → Browse All Projects