Binary Search is an efficient Divide and Conquer search algorithm over sorted arrays that repeatedly cuts the search interval in half, achieving O(log N) lookup speed.
low = 0, high = 9 -> mid = Math.floor((0 + 9) / 2) = 4. Compare arr[4] (16) with target 23.
arr[mid] = 16 < 23 -> Target must be in right half [5..9]. Set low = mid + 1 = 5.low = 5, high = 9 -> mid = Math.floor((5 + 9) / 2) = 7. Compare arr[7] (56) with target 23.
arr[mid] = 56 > 23 -> Target must be in left sub-half [5..6]. Set high = mid - 1 = 6.low = 5, high = 6 -> mid = Math.floor((5 + 6) / 2) = 5. Compare arr[5] (23) with target 23.
arr[mid] = 23 == 23 -> Target found at Index 5!Opening a dictionary to the middle page. If your word comes alphabetically before the middle word, you eliminate the entire right half.
~9 steps (Logarithmic)
O(log N) Logarithmic Time
O(1) Constant Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Best Case | O(1) | O(1) |
| Average Case | O(log N) | O(1) |
| Worst Case | O(log N) | O(1) |
function binarySearch(arr, target) {
let low = 0, high = arr.length - 1;
while (low <= high) {
const mid = Math.floor(low + (high - low) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}Locates primary key rows rapidly.
Finds the exact commit that introduced a bug via binary search over commit history.
Test your mastery over Divide & Conquer search bounds, midpoint calculations, and log N runtime limits.
Use `mid = low + (high - low) / 2` instead of `(low + high) / 2` to prevent integer overflow errors in C/C++/Java!
Sorted Array loaded. Enter search value to start binary pointers.