Binary Search Algorithm

Watch binary search find a target value by repeatedly splitting a sorted array in half. Adjust the array size, target, and speed, or plug in your own sorted list, then step through exactly how the algorithm converges.

Search Settings
Sorted Array
Target
Left
Right
Mid
Comparisons
0
Status
Ready
Algorithm Steps
Run a search to see each step logged here.
Time Complexity (Best)
O(1)
Time Complexity (Avg / Worst)
O(log n)
Space Complexity
O(1)

Looking into the Binary Search Algorithm Visualizer

Binary Search Algorithm - Visualizer Screenshot

Features of Binary Search Tool

FAQs

Enter a sorted array of numbers and a target value. Then, click “Start” or “Step” to watch the animation. You can also use “Reset” to generate a new random sorted list.

Visualization is the best way to absorb abstract concepts more easily. It helps you draw comparisons and improve understanding as a beginner, educator or prepare for an interview.

Yes, it is completely free.

Binary search is an efficient searching algorithm that finds the position of a target value in a sorted array by repeatedly dividing the search interval in half. It compares the target with the middle element and discards half of the remaining elements in each step, making it much faster than linear search.

On this page, you can watch it in action: the visualizer shows the left, right, and middle pointers moving, highlights comparisons, and counts steps so you can clearly see how the search space shrinks until the target is found (or not found).

Here is a clear 7-step breakdown of how binary search works (as visualized on this tool):

  • Start with a sorted array and set two pointers: left = 0 and right = array length – 1.
  • While left ≤ right:
  • Calculate the middle index: mid = left + (right – left) / 2 (integer division, avoids overflow).
  • Compare the middle element with the target: If array[mid] == target, the element is found → return mid.
  • If array[mid] < target, search the right half → set left = mid + 1.
  • If array[mid] > target, search the left half → set right = mid – 1.
  • Repeat steps 3–4 until the target is found or left > right.
  • If the loop ends without finding the target, return -1 (not found).
  • The algorithm finishes in at most O(log n) steps.

    Try adjusting the array size, target, and speed in the visualizer above to see these steps happen live.

Binary search is the algorithm itself. It is a classic example of the divide-and-conquer strategy. It does not use or depend on another algorithm — it simply applies the principle of repeatedly halving the search space in a sorted collection.
(Some people confuse it with binary trees or other structures, but binary search works on sorted arrays/lists, not trees. This visualizer shows the array-based version clearly.)

Binary search is the algorithm itself. It is a classic example of the divide-and-conquer strategy. It does not use or depend on another algorithm — it simply applies the principle of repeatedly halving the search space in a sorted collection.
(Some people confuse it with binary trees or other structures, but binary search works on sorted arrays/lists, not trees. This visualizer shows the array-based version clearly.)

Use binary search when:

  • The data is sorted (or can be treated as monotonic — increasing or decreasing).
  • You need very fast lookup: O(log n) time instead of O(n).
  • The problem involves finding a boundary, minimum/maximum value, or checking a condition that can be evaluated at the middle point.


Common LeetCode scenarios where binary search is the right choice:

  • Basic search in sorted array (LeetCode 704)
  • Find first/last occurrence of element (LeetCode 34)
  • Search in rotated sorted array (LeetCode 33)
  • Search a 2D matrix (LeetCode 74)
  • “Binary search on answer” problems (e.g., minimum eating speed – LeetCode 875, capacity to ship packages – LeetCode 1011)

Before coding, use this visualizer to build intuition: generate a sorted array, pick a target, and watch how quickly the pointers converge — it helps you understand why and when binary search is powerful.