Binary Search Simulator

Visualize binary search step by step. Watch the search range shrink, see how the midpoint is selected, and understand which array elements are eliminated during each iteration. Compare binary search with linear search using an interactive step counter.

Interactive visualization Step-by-step search Linear search comparison

Configure Your Search

Enter a sorted array and the value you want to find.

Enter up to 64 integers separated by commas or spaces. Values must be sorted in ascending order.
The integer you want to locate.

Binary Search Visualization

Select an iteration to see the active range and midpoint.

Ready
Current iteration
0
Low index
—
Midpoint index
—
High index
—
Array elements
Active range Midpoint Target found Eliminated

Current search explanation
Start a search to see the first midpoint calculation.

Search Results and Step Counter

Binary search result
—
Run the simulator to see whether the target exists.
Linear search result
—
Linear search comparison will appear here.
Binary search steps
—
Linear comparisons
—
Array size
—
Target value
—

Binary Search Iterations

Every midpoint calculation and range update.

Step Low Mid High Midpoint value Comparison Action
No search has been performed yet.

Binary Search vs. Linear Search

Both algorithms search for the same target. Compare the number of element comparisons needed by each method.

Binary Search

—
Element comparisons

Repeatedly divides the sorted search interval in half.

Time complexity: O(log n)

Requires the data to be sorted.

Linear Search

—
Element comparisons

Checks elements one by one until the target is found or the array ends.

Time complexity: O(n)

Does not require sorted data.

Comparison summary
Run a search to compare the two algorithms.

How Binary Search Works

Binary search is an efficient algorithm for finding a target value in a sorted array. It begins by examining the middle element of the current search interval.

1. Find the midpoint

Calculate the midpoint between the current low and high indexes. This identifies the element to compare with the target.

2. Compare values

If the midpoint equals the target, the search succeeds. If the target is smaller, continue in the left half.

3. Eliminate a half

If the target is larger, continue in the right half. Repeat until the target is found or no search range remains.

Midpoint calculation
mid = floor((low + high) / 2)

The midpoint is calculated using integer division, so it always identifies a valid array index.

Understanding Search Complexity

Binary search has logarithmic time complexity because each iteration eliminates approximately half of the remaining search interval. For an array containing n elements, the maximum number of comparisons grows proportionally to the base-2 logarithm of n.

Linear search checks elements sequentially. In the worst case, it may examine every element, giving it a time complexity of O(n).

Binary search is especially useful for large, sorted collections. Linear search remains useful for small or unsorted collections and situations where sorting the data is not practical.

Frequently Asked Questions

What is a binary search simulator?

It is an interactive learning tool that demonstrates how binary search locates a value in a sorted array. It displays the search boundaries, midpoint, comparisons, and eliminated ranges.

Does binary search require a sorted array?

Yes. Standard binary search requires the array to be sorted. Otherwise, eliminating one half based on the midpoint comparison may discard the target.

What is the midpoint in binary search?

The midpoint is the index at the center of the current search interval. It is calculated from the low and high indexes using integer division.

What happens when the target is not found?

The search continues narrowing the interval until the low index becomes greater than the high index. At that point, no possible positions remain and the target is not present.

Is binary search always faster than linear search?

Not necessarily. Binary search generally requires fewer comparisons on large sorted arrays, but linear search can be simpler and competitive on small or unsorted collections.

Can binary search handle duplicate values?

Yes. This simulator can search arrays containing duplicate values. Standard binary search may return any matching occurrence, not necessarily the first or last one.

How many elements can I search?

This version accepts up to 64 integer values in a single array.

Related Binary and Algorithm Tools

Explore other binary and computational tools.