The best answers to the question “What does O(log n) mean exactly?” in the category Dev.

__QUESTION__:

I am learning about Big O Notation running times and amortized times. I understand the notion of *O(n)* linear time, meaning that the size of the input affects the growth of the algorithm proportionally…and the same goes for, for example, quadratic time *O(n ^{2})* etc..even algorithms, such as permutation generators, with

*O(n!)*times, that grow by factorials.

For example, the following function is *O(n)* because the algorithm grows in proportion to its input *n*:

```
f(int n) {
int i;
for (i = 0; i < n; ++i)
printf("%d", i);
}
```

Similarly, if there was a nested loop, the time would be O(n^{2}).

But what exactly is *O(log n)*? For example, what does it mean to say that the height of a complete binary tree is *O(log n)*?

I do know (maybe not in great detail) what Logarithm is, in the sense that: log_{10} 100 = 2, but I cannot understand how to identify a function with a logarithmic time.

__ANSWER__:

`O(log N)`

basically means time goes up linearly while the `n`

goes up exponentially. So if it takes `1`

second to compute `10`

elements, it will take `2`

seconds to compute `100`

elements, `3`

seconds to compute `1000`

elements, and so on.

It is `O(log n)`

when we do divide and conquer type of algorithms e.g binary search. Another example is quick sort where each time we divide the array into two parts and each time it takes `O(N)`

time to find a pivot element. Hence it `N O(log N)`

__ANSWER__:

I cannot understand how to identify a function with a log time.

The most common attributes of logarithmic running-time function are that:

- the choice of the next element on which to perform some action is one of several possibilities, and
- only one will need to be chosen.

or

- the elements on which the action is performed are digits of n

This is why, for example, looking up people in a phone book is O(log n). You don’t need to check *every* person in the phone book to find the right one; instead, you can simply divide-and-conquer by looking based on where their name is alphabetically, and in every section you only need to explore a subset of each section before you eventually find someone’s phone number.

Of course, a bigger phone book will still take you a longer time, but it won’t grow as quickly as the proportional increase in the additional size.

We can expand the phone book example to compare other kinds of operations and *their* running time. We will assume our phone book has *businesses* (the “Yellow Pages”) which have unique names and *people* (the “White Pages”) which may not have unique names. A phone number is assigned to at most one person or business. We will also assume that it takes constant time to flip to a specific page.

Here are the running times of some operations we might perform on the phone book, from fastest to slowest:

**O(1) (in the worst case):**Given the page that a business’s name is on and the business name, find the phone number.**O(1) (in the average case):**Given the page that a person’s name is on and their name, find the phone number.**O(log n):**Given a person’s name, find the phone number by picking a random point about halfway through the part of the book you haven’t searched yet, then checking to see whether the person’s name is at that point. Then repeat the process about halfway through the part of the book where the person’s name lies. (This is a binary search for a person’s name.)**O(n):**Find all people whose phone numbers contain the digit “5”.**O(n):**Given a phone number, find the person or business with that number.**O(n log n):**There was a mix-up at the printer’s office, and our phone book had all its pages inserted in a random order. Fix the ordering so that it’s correct by looking at the first name on each page and then putting that page in the appropriate spot in a new, empty phone book.

For the below examples, we’re now at the printer’s office. Phone books are waiting to be mailed to each resident or business, and there’s a sticker on each phone book identifying where it should be mailed to. Every person or business gets one phone book.

**O(n log n):**We want to personalize the phone book, so we’re going to find each person or business’s name in their designated copy, then circle their name in the book and write a short thank-you note for their patronage.**O(n**A mistake occurred at the office, and every entry in each of the phone books has an extra “0” at the end of the phone number. Take some white-out and remove each zero.^{2}):**O(n · n!):**We’re ready to load the phonebooks onto the shipping dock. Unfortunately, the robot that was supposed to load the books has gone haywire: it’s putting the books onto the truck in a random order! Even worse, it loads all the books onto the truck, then checks to see if they’re in the right order, and if not, it unloads them and starts over. (This is the dreaded**bogo sort**.)**O(n**You fix the robot so that it’s loading things correctly. The next day, one of your co-workers plays a prank on you and wires the loading dock robot to the automated printing systems. Every time the robot goes to load an original book, the factory printer makes a duplicate run of all the phonebooks! Fortunately, the robot’s bug-detection systems are sophisticated enough that the robot doesn’t try printing even more copies when it encounters a duplicate book for loading, but it still has to load every original and duplicate book that’s been printed.^{n}):

__ANSWER__:

The explanation below is using the case of a fully *balanced* binary tree to help you understand how we get logarithmic time complexity.

Binary tree is a case where a problem of size n is divided into sub-problem of size n/2 until we reach a problem of size 1:

And that’s how you get O(log n) which is the amount of work that needs to be done on the above tree to reach a solution.

A common algorithm with O(log n) time complexity is Binary Search whose recursive relation is T(n/2) + O(1) i.e. at every subsequent level of the tree you divide problem into half and do constant amount of additional work.

__ANSWER__:

Many good answers have already been posted to this question, but I believe we really are missing an important one – namely, the illustrated answer.

What does it mean to say that the height of a complete binary tree is O(log n)?

The following drawing depicts a binary tree. Notice how each level contains double the number of nodes compared to the level above (hence *binary*):

Binary search is an example with complexity `O(log n)`

. Let’s say that the nodes in the bottom level of the tree in figure 1 represents items in some sorted collection. Binary search is a divide-and-conquer algorithm, and the drawing shows how we will need (at most) 4 comparisons to find the record we are searching for in this 16 item dataset.

Assume we had instead a dataset with 32 elements. Continue the drawing above to find that we will now need 5 comparisons to find what we are searching for, as the tree has only grown one level deeper when we multiplied the amount of data. As a result, the complexity of the algorithm can be described as a logarithmic order.

Plotting `log(n)`

on a plain piece of paper, will result in a graph where the rise of the curve decelerates as `n`

increases: