Binary search
Searching a sorted list by cutting it in half at every step.
Example
To find 70 between 1 and 100: check 50 (higher), then 75 (lower), then 62... You find it in 7 steps.
Test yourself: Binary search
What is an algorithm
Looking up a word in a dictionary by opening in the middle is the same method as…
Each time you open it, you discard half: with 1,000 pages that's about 10 steps. Page by page it could be up to 1,000.
Big O notation
Binary search only works if the list is ___.
In [90, 10, 70, 40], checking the middle doesn't say which half to discard: sort it first.
Big O notation
Binary search has O(___) complexity.
Each step discards half: with 1 million items about 20 steps are enough, since 2 to the 20th is roughly 1 million.
0/3
â–¶ PlayLessons that use it
What is an algorithm
You already use algorithms
Big O notation
How much does it grow?
Time complexity
Count steps, not seconds
Arrays
Adjacent cells
Linked lists
Links of a chain
Dynamic lists
An array that grows by itself
Binary search trees
Smaller on the left, larger on the right
Binary search in depth
2 pointers closing in
Boundaries with binary search
Where to insert and where a run starts
Searching with a hash
From O(n²) to O(n) with a table
Choosing how to search
Brute force or using the order
What makes a good sort
5 things you ask of it
Divide and conquer
Split, solve and combine
Dense and sparse indexes
The book's index
