Learning the 'divide and conquer' method to find items much faster in a sorted list.
Imagine you are looking for a single word in a 1,000-page dictionary. If you started on page one and read every word, it would take weeks! What if you could find that word in just 10 flips?
Before we learn the shortcut, we need to understand the long way. Linear Search is when you check every item in a list, one by one, starting from the beginning. If you are looking for a specific card in a shuffled deck of cards, you might have to look at all cards before you find it. In Computer Science, we say this takes 'linear time' because the more items you have, the longer it takes in a straight line. While this works for small lists, it is incredibly slow for big data like Google searches or Instagram followers.
Quick Check
If you have a list of 500 names and the name you want is at the very end, how many steps does a Linear Search take?
Answer
500 steps
Binary search has one strict rule: the list MUST be sorted. This means numbers must go from smallest to largest () or words must be in alphabetical order (). If the list is messy, the algorithm gets lost. Think of a phone book; because the names are sorted, you don't have to look at every page. You can jump around because you know exactly which direction the 'S' names are compared to the 'M' names. This 'direction' is what makes binary search so powerful.
Which of these lists can be used for a Binary Search? 1. 2.
Step 1: Look at the first list. The numbers jump up and down. It is unsorted. Step 2: Look at the second list. Every number is larger than the one before it. It is sorted. Result: Only list #2 works for Binary Search!
The strategy of binary search is called Divide and Conquer. Instead of checking one by one, you jump to the exact middle of the list. You ask: 'Is my target higher or lower than this middle item?' If your target is higher, you instantly 'throw away' the entire bottom half of the list. You then repeat this with the remaining half. Every single step you take cuts the number of items you have to search in half! Mathematically, if you have items, you only need about steps to find your target.
Find the number in this sorted list: 1. Find the middle: The middle number is . 2. Compare: Is bigger or smaller than ? It's bigger! 3. Discard: Throw away and . Now you only have . 4. Repeat: The new middle is . Is bigger or smaller? Smaller! 5. Discard: Throw away and . You are left with . Found it in 2 steps!
Quick Check
If you start with 100 items, how many items are left to search after your first 'split' in binary search?
Answer
50 items
The difference in speed is mind-blowing. For a list of items, Linear Search might take steps. Binary Search takes only steps. If you had items (one million), Linear Search could take a million steps, but Binary Search would finish in just steps! This is because is over a million. This efficiency allows computers to search through billions of records in the blink of an eye.
Imagine a game where I pick a number between and . 1. Using Linear Search, you guess It could take guesses. 2. Using Binary Search, you guess . I say 'Higher'. 3. You guess (halfway between and ). I say 'Lower'. 4. By continuing to split the range, you will find my number in exactly guesses or fewer, because .
What is the most important requirement for a Binary Search to work?
If you have 16 items, what is the maximum number of steps Binary Search will take?
Linear search is usually faster than binary search for lists with 1 million items.
Review Tomorrow
In 24 hours, try to explain to a friend why you can't use binary search on a shuffled deck of cards.
Practice Activity
Play the 'Guess My Number' game with a partner. Pick a number between 1 and 100 and see if they can find it in 7 guesses or fewer using the binary search strategy!