Finding the runtime of an $O(n \log_2(n))$ function

76 Views Asked by At

A function is $O(n \log_2(n))$. If it takes 1 second for the function to run when $n = 1000000$, then how long does the function take to run when $n = 8000000$? The answer is $92$ seconds.

Initially, I was trying to use a constant to multiply the left-hand side of the equation by that would set $n=8000000$ and multiply the right-hand side of the equation by the same constant. However, this doesn't seem to work very well. Here's what I have tried:

$$1000000\log_2(1000000) = 10$$ $$8 \cdot 1000000 \log_2(8 \cdot 1000000) = a \cdot 10$$ $$8 \cdot 1000000 \cdot (\log_2(8) + \log_2(1000000)) = a \cdot 10$$ $$8 \cdot 1000000 \cdot (3 + \log_2(1000000)) = a \cdot 10$$

From here, I am not sure how to create some constant $a$ on the left-hand side of the equation, so I can set $a$ on the right side equal to $a$ on the left side and scale $10$ to $92$ to derive the answer. My approach to this problem might be wrong to begin with, though.

1

There are 1 best solutions below

0
On

I think the idea is a constant multiple of $n\log n$ with $n=1000000$ will give you 1 second ($k\cdot 10^6 \log 10^6 = 1$). What is the multiple? Once you find it, plug in $n=8000000$.

In other words, something that is O(nlogn) won't take 8 times longer when $n$ is 8 times bigger. Instead it will take $\frac{8\cdot10^6\log(8\cdot10^6)}{ 10^6\log 10^6}$ times longer.

Technically, none of the above is true. This is all at a heuristic level. You're saying "Well n is pretty big so let's pretend the asymptotic behavior sets in". Technically asymptotic behavior tells you nothing about what happens for any finite $n$.