AlgoPrecision

Search Visualizer

Searching

Compare direct scanning with range-halving search. Each step shows the active search space, eliminated indexes, and the synced code line.

O(log n)
Index 018
Index 129
Index 236
Index 342
Index 454
Index 567
Index 673
Index 791

Step Note

Sort the values and search the full range.

This visualizer displays a sorted copy of the input values.

Details
function binarySearch(array: number[], target: number) {
  let left = 0
  let right = array.length - 1
  while (left <= right) {
    const mid = Math.floor((left + right) / 2)
    if (array[mid] === target) return mid
    if (array[mid] < target) {
      left = mid + 1
    } else {
      right = mid - 1
    }
  }
  return -1
}