- Home
- /
- Computational Thinking
- /
- Computational Thinking
- /
- Binary Search
Binary Search
Also known as: bisection search, half-interval search
Grade 9-12
View on concept mapAn efficient algorithm for finding a target value in a sorted list by repeatedly halving the search range. Binary search is orders of magnitude faster than linear search for large sorted datasets.
Definition
An efficient algorithm for finding a target value in a sorted list by repeatedly halving the search range. At each step, compare the target to the middle element: if equal, the search is done; if smaller, search the left half; if larger, search the right half.
π‘ Intuition
Looking up a word in a dictionary: open to the middle, then go left or right depending on where your word falls.
π― Core Idea
Each comparison eliminates half the remaining possibilities β O(log n) vs O(n) for linear search.
Example
Formula
π Why It Matters
Binary search is orders of magnitude faster than linear search for large sorted datasets. Searching 1 billion sorted items takes at most 30 comparisons with binary search, compared to up to 1 billion with linear search.
π Hint When Stuck
To perform binary search, set low = 0 and high = length - 1. Compute mid = (low + high) / 2. If the target equals the middle element, you are done. If the target is less, set high = mid - 1. If greater, set low = mid + 1. Repeat until found or low > high.
Formal View
Related Concepts
π§ Common Stuck Point
Binary search only works on sorted data β applying it to unsorted data gives wrong results.
β οΈ Common Mistakes
- Applying binary search to an unsorted list, which produces incorrect results
- Computing the midpoint as (low + high) / 2, which can cause integer overflowβuse low + (high - low) / 2 instead
- Getting the boundary updates wrong (using mid instead of mid+1 or mid-1), causing infinite loops
Frequently Asked Questions
What is Binary Search in CS Thinking?
An efficient algorithm for finding a target value in a sorted list by repeatedly halving the search range. At each step, compare the target to the middle element: if equal, the search is done; if smaller, search the left half; if larger, search the right half.
What is the Binary Search formula?
When do you use Binary Search?
To perform binary search, set low = 0 and high = length - 1. Compute mid = (low + high) / 2. If the target equals the middle element, you are done. If the target is less, set high = mid - 1. If greater, set low = mid + 1. Repeat until found or low > high.
Prerequisites
Next Steps
How Binary Search Connects to Other Ideas
To understand binary search, you should first be comfortable with searching, array and efficiency. Once you have a solid grasp of binary search, you can move on to linear search.