Lessons › coding and computer science · taught by Leo
Linear vs binary search: finding items fast
Learn when to use linear search or binary search, and why sorted lists matter for fast searching.
Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.appFind the target fast
When you need to find one thing in a list, you can search in different ways.
The big question is: do you check every item, or can you skip a lot of them?
Two search strategies
Linear search checks items one by one. Binary search cuts the search space in half each time, but only works when the list is sorted.
Linear search is simple: start at the beginning and check each item.
Binary search is faster on sorted data: compare with the middle, then throw away half.
So the tradeoff is easy method versus faster method.
Predict the better fit
Which search method needs the list to be sorted first?
- Linear search
- Binary search
- Both need sorting
Show answer
Binary search: Yes. Binary search depends on order, because it decides which half to keep.
Linear search: one by one
Here is a linear search for 7 in a list.
You check each item in order until you find 7.
If 7 were not there, you would reach the end and stop.
Quick check: next move
In linear search, what should you do next after checking an item that is not the target?
- Jump to the middle
- Check the next item
- Restart the list
Show answer
Check the next item: Correct. Linear search moves to the next item one by one.
Why binary search is fast
Binary search works because every comparison removes half the remaining list. That makes the number of checks grow very slowly as the list gets bigger.
If the list is sorted, the middle item tells you where the target can still be.
If the target is smaller, ignore the right half. If it is bigger, ignore the left half.
That shrinking is why binary search is much faster on large lists.
Binary search: halve the list
Now watch binary search on a sorted list.
You look at the middle first, not the beginning.
Then you keep only the half that could still contain the target.
Quick check: what gets cut
If the target is bigger than the middle value, what do you keep?
- The left half
- The right half
- All of the list
Show answer
The right half: Right. Bigger than the middle means the target must be on the right side.
Watch out: sorted only
This is the most common mistake: using binary search on unsorted data.
Without order, the middle does not tell you which half to throw away.
So always ask: is the list sorted first?
When to use each one
Use this checklist when you decide.
If the list is sorted, binary search is usually the smart choice.
If it is not sorted, linear search is the safe choice.
Your turn: choose the method
You have a sorted list of 64 names. Which method is best?
- Linear search
- Binary search
- Either one is the same
Show answer
Binary search: Yes. Binary search is best here because the list is sorted and large.
Remember the two-key rule
Here is the full memory trick.
Linear is simple. Binary is fast, but only with sorted data.
If you remember those two rules, you can pick the right search every time.