Developer

This experience deserves a bigger screen

The DSA Visualizer is designed for deep focus,
side-by-side code, and real-time execution insight.

On small screens, that level of clarity can't be preserved.

Open on a tablet (landscape) or desktop for the intended experience.

Desktop experience preview

Binary SearchStep-by-Step Visualization

Binary Search is the most efficient way to find an element in a sorted array. It divides the search interval in half repeatedly.

Problem Statement

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

Approach

Initialize two pointers, left and right. While left <= right, calculate the middle index. If nums[mid] equals the target, return mid. If nums[mid] < target, search the right half (left = mid + 1). If nums[mid] > target, search the left half (right = mid - 1).

Time & Space Complexity

Time complexity is O(log n) because the search space is halved in each step. Space complexity is O(1) for iterative implementation.

Interactive Visualization

Loading Interactive Visualizer...

Click "Play" or use arrow keys to step through the execution.