AiTechWorlds
AiTechWorlds
Watch Binary Search halve a sorted array each step with a live lo/mid/hi range, synced pseudocode, and multi-language code.
def binary_search(a, target):
lo, hi = 0, len(a) - 1
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == target: return mid
if a[mid] < target: lo = mid + 1
else: hi = mid - 1
return -1Binary Search repeatedly halves a sorted array by comparing the target with the middle element, discarding the half that cannot contain it.
| Best | O(1) |
| Average | O(log n) |
| Worst | O(log n) |
| Space | O(1) |
| Requires sorted | Yes |
The Binary Search Visualizer animates how Binary Search works: on a sorted array it compares the target with the middle element and discards the half that cannot contain it, repeating until found. Binary Search runs in O(log n) time, uses O(1) space, and requires the array to be sorted.
Set a target
Type the value to search for, or click New Array to randomize the sorted data and target.
Play the animation
Press Play to watch the active range (blue) shrink and the mid (yellow) move.
Step and scrub
Use Step Forward/Back or the timeline to study each halving.
Read the synced code
The highlighted pseudocode line tracks lo/mid/hi; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.