1

There are plenty of questions around O(log n) and what it actually means, not trying to open that up again.

However on this particular answer https://stackoverflow.com/a/36877205/10894153, this image makes no sense to me:

enter image description here

That being said, seeing as that answer has over 100 up votes votes and has been up for more than 2 years and no comments to indicate anything might be wrong, I assume I am misunderstanding something, hence asking here for clarification (and I can't post comments because of low reputation).

Mainly, I don't understand why O(log(n)) is 10 when n == 1024. Shouldn't this be 32, seeing as 32^2 = 1024?

Clearly this has an effect on O(n * log(n)) as well, but just need to understand why O(log(1024)) = 10

JonU
  • 83
  • 6

1 Answers1

1

The table is correct, except that the headings could be misleading because the values below them correspond to the expressions inside the big-O, rather than to the big-O themselves. But that is understandable because O-notations have the meaning of disregarding multiplicative constants.

Something similar happens with log(n). The log notation has also the meaning of disregarding the base of the logarithmic function. But that's fine in this context because:

log_b(n) = log_2(n)/log_2(b)                 ; see below why this is true

meaning that the function log_b() is just a multiplicative constant away, namely 1/log_2(b), from log_2().

And since the table is purposely emphasizing the fact that the big-O notation disregards multiplicative constants, it is fine to assume that all logs in it are 2-based.

In particular, O(log(1024)) can be interpreted as log_2(1024) which is nothing but 10 (because 2^10 = 1024).


To verify the equation above, we need to check that

log_2(n) = log_2(b)log_b(n)

By the definition of log we have to see that n is 2 to the righ-hand-side, i.e.,

n = 2^{log_2(b)log_b(n)}

but the right hand side is

{2^{log_2(b)}}^{log_b(n)} = b^{log_b(n)} = n

again by definition (applied twice).

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51