What is Binary Search?
Look at the middle of a sorted list and throw away the half that cannot contain the target.
Type a tool name, format or task
Look at the middle of a sorted list and throw away the half that cannot contain the target. Interactive step-by-step animation with JavaScript code, complexity and front-end use cases.
Loading animation...
Look at the middle of a sorted list and throw away the half that cannot contain the target. Set a low and a high pointer around the sorted list. Compare the middle value with the target. Move low or high past the middle to drop half of the list, and repeat. Time complexity: O(log n). Space complexity: O(1). Searching sorted data, finding positions in virtualized lists and even finding the commit that broke your app (git bisect).
Look at the middle of a sorted list and throw away the half that cannot contain the target.
Time: O(log n). Space: O(1).
Searching sorted data, finding positions in virtualized lists and even finding the commit that broke your app (git bisect).
No. This converter runs in your browser for ordinary use, so the input is not sent to a backend by this tool.
Keep working without searching again.
Binary search needs a sorted list (yours is sorted for you). It looks at the middle and throws away half each time.
function binarySearch(arr, target) {let lo = 0, hi = arr.length - 1;while (lo <= hi) {const mid = (lo + hi) >> 1;if (arr[mid] === target) return mid;if (arr[mid] < target) lo = mid + 1;else hi = mid - 1;}return -1;}
Tip: use the arrow keys to step and the space bar to play or pause.