Algorithm Visualizer

Binary Search Algorithm

Visualize how binary search efficiently finds elements in a sorted array.

Visualization
Binary search works by repeatedly dividing the search interval in half.
Animation Speed
Searching for: 0
Step: 0 / 0
How Binary Search Works

Binary search is an efficient algorithm for finding an item in a sorted array with a time complexity of O(log n). It works like this:

  1. Start with the middle element of the sorted array.
  2. If the target value equals the middle element, the search is complete.
  3. If the target value is less than the middle element, continue the search on the left half.
  4. If the target value is greater than the middle element, continue the search on the right half.
  5. Repeat steps 1-4 until the value is found or the search space is empty.

Complexity

  • Time Complexity: O(log n)
  • Space Complexity: O(1) for iterative implementation

Prerequisites

Binary search requires the array to be sorted before searching.