AlgoViz

Binary Search

Binary Search efficiently finds a target value in a sorted array by repeatedly dividing the search interval in half.

BeginnerTime: O(log n)Space: O(1)
Take Quiz
64
34
25
12
22
11
90
45
67
33
Speed1x

Current Step

Press Play to start the visualization


## How Binary Search Works

Binary Search is an efficient algorithm for finding an element in a sorted array. It works by repeatedly dividing the search interval in half.

### Algorithm Steps:
1. Start with the entire sorted array
2. Compare target with the middle element
3. If target equals middle, we're done
4. If target < middle, search left half
5. If target > middle, search right half
6. Repeat until found or interval is empty

### Key Characteristics:
- **Efficient**: O(log n) time complexity
- **Requires sorted array**: Must sort first if unsorted
- **Divide and conquer**: Halves search space each step

Time & Space Complexity

CaseTime Complexity
BestO(1)
AverageO(log n)
WorstO(log n)
SpaceO(1)

Practice Problems

10 problems81 total points
1

Basic Binary Search

Beginner5 pts
2

Search Insert Position

Beginner5 pts
3

First Bad Version

Beginner5 pts
4

Find First and Last Position

Intermediate8 pts
5

Find Peak Element

Intermediate8 pts
6

Search in Rotated Sorted Array

Advanced10 pts
7

Find Minimum in Rotated Sorted Array

Advanced10 pts
8

Median of Two Sorted Arrays

Advanced10 pts
9

Kth Smallest Element in Sorted Matrix

Advanced10 pts
10

Split Array Largest Sum

Advanced10 pts