Binary Search Calculator
Visualize binary search on a sorted list. Count worst-case comparisons for any array size and trace each step — low, high, mid and decision — in your browser.
Formula
max comparisons = ⌈log₂(N + 1)⌉ = ⌈log₂(1,000 + 1)⌉ = 10
Each comparison halves the remaining range, so binary search never needs more than 10 comparisons to search 1,000 elements. This grows as O(log n).
What is the Binary Search Calculator?
The ByteTools Binary Search Calculator shows how the binary search algorithm finds a value in a sorted list.
- Worst-case comparison count from the array size
- Step-by-step trace with low, high, mid and mid value
- Sorts your list ascending before searching
- Reports the found index or a clean not-found result
- Shows the ⌈log₂(N + 1)⌉ formula and O(log n) complexity
- 100% private — your list never leaves your browser
How to use the Binary Search Calculator
- 1
Pick a mode: array size for the worst-case comparison count, or search a value for a full trace.
- 2
In array-size mode, enter how many sorted elements there are and read ⌈log₂(N + 1)⌉.
- 3
In search mode, paste numbers separated by commas, spaces or new lines and type a target.
- 4
Follow the step table — low, high, mid, mid value and each go-left or go-right decision.
- 5
Read whether the target was found and after how many comparisons, then click Copy.
About the Binary Search Calculator
The ByteTools Binary Search Calculator shows how the binary search algorithm finds a value in a sorted list. Give it the size of an array to see the maximum number of comparisons it will ever need, or paste a list of numbers and a target to watch the search narrow down step by step.
It is built for computer science students, coding-interview prep and anyone learning how O(log n) search works. Each step of the trace shows the low, high and mid indices, the value at the midpoint and the decision the algorithm makes, so the halving behaviour becomes obvious.
Everything runs entirely in your browser with JavaScript. Your list is never uploaded, so the tool is private, instant and works offline. Copy the worked formula or the full step trace with one click.
Frequently asked questions
How does binary search work?
Binary search looks at the middle element of a sorted list. If it matches the target you are done; if the target is smaller you repeat on the left half, and if it is larger you repeat on the right half. Each step halves the remaining range, so it is very fast.
How many comparisons does binary search need in the worst case?
For a sorted array of N elements the worst case is ⌈log₂(N + 1)⌉ comparisons. For example 1,000 elements need at most 10 comparisons and 1,000,000 need at most 20. This logarithmic growth is why binary search runs in O(log n) time.
Why is binary search O(log n)?
Every comparison discards about half of the remaining candidates, so the number of items still in play keeps halving: N, N/2, N/4 and so on. The count of halvings needed to reach a single element is roughly log₂(N), which gives the O(log n) worst case.
Does the list have to be sorted for binary search?
Yes. Binary search relies on the list being ordered so it can decide which half to discard. This calculator sorts your input ascending before searching, so you always see the algorithm running on valid, sorted data.
How is the middle index calculated?
The midpoint is mid = ⌊(low + high) / 2⌋, using the floor of the average of the current low and high indices. The high index is inclusive, so the search range is low to high and the loop continues while low is less than or equal to high.
What happens if the target is not in the list?
The search keeps halving until the low index passes the high index, meaning the range is empty. At that point the calculator reports that the value was not found and tells you how many comparisons were made along the way.
Related tools
Logarithm Calculator
Calculate the logarithm of a number with any base, including log base 10, natural log (ln) and log base 2. See the result and an inverse check, all in your browser.
Exponent Calculator
Calculate base raised to any exponent online, including negative and fractional powers. See the result and the full expression, computed instantly in your browser.
Fibonacci Calculator
Find the nth Fibonacci number and list the sequence up to n. Uses BigInt for exact results, so even very large Fibonacci numbers stay precise in your browser.