A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Binary Search

College Depth 85 in the knowledge graph ☐ I know this ☆ Set as goal
45topics build on this
446prerequisites beneath it
See this on the map →
Array Data Structure: Representation and OperationsArrays and Lists+3 more→→B-Tree IndexesBinary Search Trees+2 more
searching binary-search divide-and-conquer sorted-arrays

Core Idea

Binary search finds a target value in a sorted array by repeatedly halving the search space. At each step, the algorithm compares the target to the middle element and eliminates half of the remaining candidates. This achieves O(log n) time complexity, a dramatic improvement over O(n) linear search for large datasets. Binary search requires that the input array be sorted, and the key insight is that sortedness allows drawing definitive conclusions about entire halves of the array.

How It's Best Learned

Implement both iterative and recursive versions. Practice on concrete sorted arrays and trace through the index arithmetic step by step. Pay careful attention to off-by-one errors in the loop bounds (< vs <=, mid+1 vs mid).

Common Misconceptions

Explainer

You already know arrays as contiguous blocks of elements accessible by index, and you have some sense of what O(n) versus O(log n) means from complexity analysis. Binary search connects these ideas: it exploits the structure of a sorted array to avoid looking at most of the elements. Imagine searching for a word in a physical dictionary. You would not start at page one and read every entry — you would open to the middle, see whether your word comes before or after that page, and immediately discard half the book. Binary search does exactly this, and the reason it works is that sortedness guarantees: if the target is less than the middle element, it cannot exist anywhere in the right half.

The algorithm maintains two pointers, low and high, that define the current search space. Initially, low is 0 and high is n-1 (the full array). At each step, compute the midpoint: mid = low + (high - low) / 2. Compare the target to the element at mid. If they match, you are done. If the target is smaller, set high = mid - 1 to search the left half. If the target is larger, set low = mid + 1 to search the right half. The search space halves with every comparison, so after k comparisons you have narrowed n elements down to n/2k. When this reaches 1, you have either found the target or determined it is absent. Solving n/2k = 1 gives k = log₂(n) — this is why binary search is O(log n). For a million elements, that is roughly 20 comparisons instead of a million.

The most treacherous aspect of binary search is getting the boundary conditions right. Should the loop condition be `while (low <= high)` or `while (low < high)`? Should you set `high = mid` or `high = mid - 1`? These choices are interdependent, and mixing conventions causes either missed elements or infinite loops. The safest approach is to use the inclusive convention: low and high both point to valid, unsearched positions, the loop runs while `low <= high`, and updates are `low = mid + 1` and `high = mid - 1`. Trace through a two-element array by hand to verify your implementation handles the edge case — this is where bugs hide.

Binary search generalizes far beyond finding exact matches in arrays. Any situation where you can define a monotonic predicate — a yes/no question where all the "yes" answers come after all the "no" answers (or vice versa) — can be binary searched. For example, "what is the smallest number of servers needed to handle this load?" can be binary searched if you can test whether k servers suffice for any given k. This generalization, often called binary search on the answer, is one of the most powerful algorithmic techniques you will encounter, and it rests on the same core insight: sortedness (or monotonicity) lets you eliminate half the candidates with a single test.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Time and Space Complexity → Binary Search

Longest path: 86 steps · 446 total prerequisite topics

Prerequisites (5)

Leads To (4)