Configure Your Search
Enter a sorted array and the value you want to find.
Binary Search Visualization
Select an iteration to see the active range and midpoint.
Search Results and Step Counter
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
Repeatedly divides the sorted search interval in half.
Time complexity: O(log n)
Requires the data to be sorted.
Linear Search
Checks elements one by one until the target is found or the array ends.
Time complexity: O(n)
Does not require sorted data.
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.
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.