A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition
by Jay Wengrow
O(log N) Explained
Let’s bring this all back to Big O Notation. In computer science, whenever we say O(log N), it’s actually shorthand for saying O(log2 N). We just omit that small 2 for convenience.
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 we end up with 1 element.
This is exactly what happens with binary search. As we search for a particular item, we keep dividing the array’s cells in half until we narrow ...
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