March 24
O(log n)
What O(log n) means and why cutting the search space in half usually leads to this cost.
What it means
O(log n) means the work grows very slowly compared with the input size.
That usually happens when each step removes a large part of the remaining problem.
Common shape
The classic example is binary search.
You do not scan item by item.
You inspect the middle and decide which half is still relevant.
When it matters
O(log n) often appears when:
- the data is ordered
- you can discard half the search space each step
- the next decision depends on the current result
Better question
Instead of memorizing “binary search is O(log n),” ask:
- can I remove half the problem each step?
- does the data shape allow that without breaking the logic?
- is the input already ordered, or do I pay that cost first?
Share this page
Copy the link manually from the field below.