Binary Search
Understand the power of O(log n). Visualize dividing the search space in half to find targets in sorted arrays instantly.
Binary Search Algorithm
Searching for 23 in sorted array
Start: Search for 23 in array. Set low = 0, high = 9
Start: Search for 23 in array. Set low = 0, high = 9
Current array: [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] (length: 10)
Halve the search space
Order the steps of binary search on a sorted array.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
What precondition is REQUIRED for Binary Search?
Write the code
Search Insert Position
The short version
Think of looking up a word in a dictionary. You open to the middle. If your word is after the page you see, you ignore the first half completely. You keep splitting the remaining pages in half until you find it.
In more depth
Divide and Conquer
Binary Search is the most efficient way to search a Sorted array.
This eliminates half the search space in every step, resulting in O(log n) complexity.
Precondition: The data MUST be sorted.
When to reach for this
- Search in a **Sorted** array
- Find a value where a condition changes (False -> True)
- Search space can be divided in half (Monotonic function)
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required