O(log N) Explained
Let’s bring this all back to Big O notation. In this book, whenever we say O(log N), imagine that we’re saying O(log2 N). In Ignoring Constants, we’ll discuss why we’re dropping the base of 2 in our notation. But for now, when you see O(log N), treat it in your mind as O(log2 N).
With this in mind, we can now explain precisely what O(log N) means.
Recall that Big O notation resolves the key question: if there are N data elements, how many steps will the algorithm take?
O(log N) means that for N data elements, the algorithm would take log2 N steps. If there are 8 elements, the algorithm would take three steps, since log2 8 = 3.
Said another way, if we keep dividing the 8 elements in half, it would take us three steps until ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access