Learning Rooms/○ Level 3 · Innovator/Computing and algorithms
Algorithms: Binary Search
HackerRank · Length: see YouTube · captions on YouTube · nothing loads until you press play
By the end you can…
- Implement linear search and binary search and count their comparisons.
- State when binary search applies (sorted data, random access) and when it doesn't.
- Derive the worst case of ⌈log₂(n + 1)⌉ comparisons for binary search.
Watch it all: no segment has been checked by hand for this video yet.
1.Predict before you press play
Binary search on a sorted list of 1,000,000 items. Roughly how many comparisons in the worst case?
Pick one before you press play. A wrong guess is fine: it is what makes the video stick.
2.See the idea first
Play with this diagram for a minute. It gives you something to hang the video on.
Loading the interactive…
Can't use the diagram? Read a description instead
The visualiser shows a sorted list of part numbers as a strip of cells. Linear search colours cells one by one from the left. Binary search probes the middle, then the middle of the remaining half, shading the range still in play. With 31 parts binary search never needs more than 5 comparisons; linear search may need 31.
3.Watch, actively
Walks through binary search on a sorted array and the off-by-one details that make or break it.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
Watch for…
- → Watch the low, high and mid indexes on each step, and how the loop ends when the target is missing.
Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.
Can't stream it? Read the idea instead
In a sorted list you can find a value by checking the middle, then throwing away the half that cannot contain it, again and again. Each step halves the search, so a million items need only about twenty checks. Getting the boundaries exactly right is the hard part.
What the video covers (our own note, written when we verified it): Walks through binary search on a sorted array and the off-by-one details that make or break it.
Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.
Read it instead
- Lesson: Halve it again: binary search · The full lesson explains the idea in text, worked examples and diagrams, with no video needed.
- Engineering Atlas: Algorithms, searching and complexity · Matching each camera frame to the nearest force reading in a long robot log takes seconds with sorted data and binary search, but hours if every pair is compared.
- Field Guide: Amdahl's law · A short reference entry on the same idea.
Jot down one thing that surprised you and one question you still have.
4.Practise what you just saw
These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.
Practice 1 · a fresh version every time
A sorted list holds 50 calibration points. Binary search compares the target with one middle element per loop and halves the range each time. In the worst case (including a target that is not there), how many elements does it compare with the target?
Practice 2
Linear search on an unsorted list of 4,096 sensor ids, looking for one that is absent. How many comparisons?
Practice 3
A student writes 'while lo < hi:' instead of 'while lo <= hi:'. What goes wrong?
Practice 4 · a fresh version every time
A sorted list holds 500 part numbers. Binary search compares the target with one middle element per loop and halves the range each time. In the worst case (including a target that is not there), how many elements does it compare with the target?
5.Try it yourself
No simulation models this idea yet. The lesson below has an interactive diagram instead.
6.Where engineers use it
Calibration tables in embedded code are often stored sorted, and a binary search finds the right segment to interpolate in a handful of steps, fast enough to run thousands of times a second.
From the applied view in the lesson Halve it again: binary search. Checked by automated tests only.
7.Build something with it
Follow-up project
Use Python's bisect.bisect_left to find where a measured temperature falls in a sorted calibration table and interpolate between the two neighbouring entries. Check your own binary_search against bisect on 1,000 random targets.
Where this video lives
- Halve it again: binary search · Level 3, week 5
In the course catalogue: CSE-320 Algorithms and numerical computation
The video is by HackerRank, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.