Recently I have been working through a textbook question that states the following:
An algorithm takes 0.5 ms for input size 100. How long will it take for input size 500 (assuming that low-order terms are negligible) if the running time is
a. linear
b. O(N log N )
c. quadratic
d. cubic
Now I know how to get Linear as $500/100 = 5$ thus $0.5ms * 5 = 2.5ms$
What I don't understand is how do I scale this answer of 2.5ms linearly to quadratic, nlogn, and cubic? Are there theorems or expressions that I am missing?
The way I would go about $N^2$ is that $2.5ms^2 $ is 6.25 ms which is not the answer in the answer key(ans = 12.5). and i'm not sure what I would start with for $nlogn$
What does it mean when the question says this?
(assuming that low-order terms are negligible)
Any explanation would be appreciated.
When we say that an algorithm runs in $O(N \log(N))$ that means that there exists a constant $C$ such that the time of execution $T(n)$ (as a function of the size $n$ of the input) is such that $T(n) \leq C n \log(n)$. And this is not very precise. Often we implicitly mean that the algorithm runs in $\Theta(N \log(N))$, which means that there exists two constants $C_1$ and $C_2$ such that $C_1 n \log(n) \leq T(n) \leq C_2 n \log(n)$.
That being said, to answer your question we will consider that the running time of your algorithm is $T(n)=C n \log(n)$.
Then $T(500)= 500C \log(500)$ and $T(100)=100C\log(100)$. Then $100C=\dfrac{T(100)}{\log(100)}$.
You get that $$T(500)=5 (100C) \log(500) = 5 \dfrac{\log(500)}{\log(100)}T(100)\approx 5 \cdot 1.35 \cdot T(100) = 6.75 \cdot T(100) = 3.4 \textrm{ms}$$
For the other complexities the computations are easier:
quadratic: $T(n) = C n^2$ gives $$\dfrac{T(500)}{T(100)}=\dfrac{(500^2)}{100^2}=5^2=25$$ Then $T(500)=25 \cdot 0.5 = 12.5 \textrm{ms}$
cubic: $T(n) = C n^3$ gives $$\dfrac{T(500)}{T(100)}=\dfrac{(500^3)}{100^3}=5^3=125$$ Then $T(500)=125 \cdot 0.5 = 62.5 \textrm{ms}$
Of course these values are only approximations and only give a rough idea of the running time you can expect.