

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 7
click for more info
Not enough gems
Cost: 6 gems
1: Big O Notation
incomplete
2: O(n) - Order 'n'
incomplete
3: O(n^2) - Order 'N Squared'
incomplete
4: N^2 Quiz
incomplete
5: O(nm)
incomplete
6: Constants Don't Matter
incomplete
7: Constants Quiz
incomplete
8: Order 1
incomplete
9: Order Log N
incomplete
10: Name Count
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
O(log(n)) algorithms are only slightly slower than O(1), but much faster than O(n). They do grow according to the input size, n, but only according to the log of the input.
O(n):
| n | time |
|---|---|
| 8 | 8 ms |
| 64 | 64 ms |
| 1024 | 1024 ms |
| 1048576 | 1048576 ms |
O(log(n)):
| n | time |
|---|---|
| 8 | 3 ms |
| 64 | 6 ms |
| 1024 | 10 ms |
| 1048576 | 20 ms |
A binary search algorithm is a common example of an O(log(n)) algorithm. Binary searches work on a pre-sorted list of elements.
Given two inputs:
n elements sorted from least to greatesttarget value:Do the following:
n - 1.(low + high) // 2, which is the greatest integer less than or equal to (low + high) / 2list[median] == target, return Truelist[median] < target, set low to median + 1median - 1 FalseAt each iteration of loop, we halve the list. Which makes the algorithm O(log(n)). In other words, to add one more step to the runtime, we'd have to double the size of the input. Binary searches are fast.
We have a popular influencer using our LockedIn app, and she needs to be able to quickly search for posts from a particular day. This function will be the backbone of her search screen.
Complete the binary_search function. It should follow the algorithm as described above.
The input array arr is already sorted for you!