Binary Search explained step-by-step: interval halving mechanics, overflow-safe middle index calculation, edge cases, complexity analysis, and JavaScript implementation.
Binary Search is a foundational divide-and-conquer search algorithm designed exclusively for sorted collections. Instead of testing elements sequentially, it checks the element at the midpoint of the search interval and determines whether the target lies in the left half or right half.
By halving the remaining search space with every single comparison, Binary Search can pinpoint any target in a 1,000,000-element sorted array in at most 20 comparisons (log₂ 1,000,000 ≈ 19.93). It operates in O(log n) time and requires strictly O(1) auxiliary space in its iterative formulation.
Set pointer `low = 0` and `high = length - 1`, defining the active search boundary.
Calculate `mid = low + Math.floor((high - low) / 2)`. Using `low + (high - low)/2` instead of `(low + high)/2` protects against 32-bit signed integer overflow in languages like C++, Java, and Rust.
If `arr[mid] === target`, return `mid` immediately. If `arr[mid] < target`, discard the left half by setting `low = mid + 1`. If `arr[mid] > target`, discard the right half by setting `high = mid - 1`.
Continue halving the window while `low <= high`. If `low > high`, the target does not exist in the collection; return -1.
1function binarySearch(arr, target) {2 let low = 0;3 let high = arr.length - 1;4 5 while (low <= high) {6 // Prevent 32-bit integer overflow: low + (high - low) / 27 const mid = Math.floor(low + (high - low) / 2);8 9 if (arr[mid] === target) {10 return mid; // Target found at index mid11 }12 13 if (arr[mid] < target) {14 low = mid + 1; // Discard left half, search right sub-interval15 } else {16 high = mid - 1; // Discard right half, search left sub-interval17 }18 }19 20 return -1; // Target not found in the array21}22 23// Example usage:24// binarySearch([2, 5, 8, 12, 16, 23, 38, 56, 72, 91], 23); // 525// binarySearch([2, 5, 8, 12, 16, 23, 38, 56, 72, 91], 40); // -1Play through every comparison, swap, and state change, adjust the speed, or enter custom inputs to test edge cases.
The full interactive roadmap is unlocking — these specialized modules land in upcoming releases.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
Explore the full catalog — 30+ algorithms with more unlocking every release.
Binary Search ready — searching sorted array for target 42 with range [0..9].
Comparisons
0
Range Size
10
Target
42
Array Size
10
Test your understanding of Binary Search complexity and step mechanics.
Q1.What is the mandatory prerequisite requirement for Binary Search to work correctly?
Q2.Why is `low + Math.floor((high - low) / 2)` preferred over `Math.floor((low + high) / 2)`?
Apply Binary Search to real coding interview questions.
Given an array of integers nums which is sorted in ascending order, search for target.
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.