UtilityToolsLab

© 2026 UtilityToolsLab. Built and maintained by the UtilityToolsLab Team.

Free eBooks·About·Changelog·Privacy Policy·Terms of Service·Report a bug
HomeCompetitive ProgrammingBinary Search

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerBitmask PlannerPrime FactorsMEX CalcInterval MergerMatrix RotationPBDS GeneratorSegment TreeGraph VisualizerStress TesterModulo CalcConvex HullPath FinderOffline JudgeBig-O AnalyzerCombinatoricsDP Table BuilderExtended GCDSieve VisualizerSorting VisualizerSparse Table RMQUnion-Find DSU

Binary Search Step Visualizer

Step through binary search on your own sorted array. Highlights lo, hi and mid at each probe. Play, pause or step manually. O(log n) worst-case shown.

You Might Also Like

All Competitive Programming

Code Formatter

Format and beautify C++, Java, and Python code with consistent indentation. 100% client-side — no code leaves your browser.

Matrix Generator

Generate grid/matrix inputs for competitive programming. Random, zeros, identity, or sequential fill. Outputs in multiple formats.

Complexity Calc

Estimate Big-O operations and runtime for any N. Interactive reference table for all complexity classes from O(1) to O(N!).

Bit Visualizer

Toggle 32/64-bit grids. Click bits to flip them live and see decimal recalculate. Shows popcountll, clzll, ctzll, and MSB instantly.

Binary search is one of the first algorithms every competitive programmer memorises, and one of the easiest to get wrong under contest pressure. An off-by-one in the loop condition or a wrong update to lo or hi produces a result that is correct on the happy path and silently wrong on boundary inputs. Binary Search Step Visualizer walks through every probe on your own array so the update logic is visible at each stage rather than inferred from the output.

Type any sorted integer array and a target value, then step through the algorithm manually or press Play to run it at 800 ms per step. Each probe shows the current lo, hi and mid values and explains whether the search moves left, moves right, or terminates.

Walkthrough: Finding 14 in a 10-Element Array

Load Sample fills the array with 1 3 5 7 9 11 14 18 23 27 and sets the target to 14. Step 1 opens with “Start: search range [0 … 9], target = 14." The first probe lands at mid = 4 (value 9). Since 9 < 14, the next step shows “arr[4] = 9 < 14. Move right → lo = 5." The second probe is mid = 7 (value 18). Since 18 > 14, the range narrows to [5 … 6]. The third probe hits mid = 5 (value 11), moves right again, and the fourth probe lands on mid = 6 (value 14) — found in 4 probes out of the 5 worst-case maximum for 10 elements.

Edge Cases and Tricky Inputs

  • Strictly sorted, no duplicates required. The standard binary search returns one matching index. If your array contains duplicates, the index returned is unspecified — it will be whichever mid happens to equal the target first. For lower-bound or upper-bound problems, use a modified form that does not terminate on equality.
  • Maximum 32 elements. Entering more than 32 numbers shows the message “Maximum 32 elements allowed." This keeps every cell legible on a phone screen. For a 32-element array the worst case is 6 probes (⌈log₂ 32⌉ = 5, plus the final termination check).
  • Not-found path. Searching for a value absent from the array shows every narrowing step until the range becomes empty, ending with “Search range empty. X is not in the array." This is worth stepping through at least once — the not-found case is where most off-by-one bugs hide.
  • Mid formula. The visualiser uses mid = ⌊(lo + hi) / 2⌋, which overflows on 32-bit integers when lo + hi > 2³¹ − 1. The safe alternative lo + ⌊(hi − lo) / 2⌋ is equivalent but avoids that overflow; the difference is invisible at the array sizes this tool handles but matters in production code.

Input

Step 1 / 5

0
1
1
3
2
5
3
7
4
9
mid
5
11
6
14
7
18
8
23
9
27
midsearch rangeeliminatedfound
Start: search range [0 … 9], target = 14.
lo = 0hi = 9mid = 4

Time: O(log n)  · Space: O(1) iterative  · Prerequisite: strictly sorted, no duplicates

mid = ⌊(lo + hi) / 2⌋ — integer division floors towards zero. For a 32-element array the worst case is 6 probes (⌈log₂ 32⌉ = 5, but the termination step is probe 6). The visualiser steps through every probe including the final range-empty check.