Finding out how many time units will each algorithm spend for sorting an array of $1\,000\,000$ objects

82 Views Asked by At

Question: You have found empirically that the implemented sorting methods $A$ of complexity $\Theta(n^3)$ and $B$ of complexity $\Theta(n^2 \log n)$ spent $2$ and $10$ time units, respectively, to sort an array of $100$ objects. Find out how many time units will each algorithm spend for sorting an array of $1\,000\,000$ objects?

For this question can I say that the time spent by these implementations can be written as $T_A(n) = c_A n^3$ and $T_B(n) = c_B n^2 \log n$, therefore continue to solve $c$ using given numbers?

2

There are 2 best solutions below

2
On

If you are just finding an approximate time, then

$\cfrac{t1}{t2} = \cfrac{(n_1)^3}{(n_2)^3}$

$\cfrac{t1}{t2} = \cfrac{(n_1)^2 \log n_1}{(n_2)^2 \log n_2}$

Why approximate?

Because this theta notation just gives assymptotic behaviour.
In above calculation i have assumed that $k*n^3$ is the number of operations in first case, which is not exactly true. It can have lower degree terms also.

0
On

As stated, this question has no solution. Possibly the intent is to work with constants as you suggest. However, an algorithm that takes $$ T_{A,1}(n) = \begin{cases} 2 & \text{if }n < 10^{10} \\ n^3 & \text{if } n \ge 10^{10}\end{cases} $$ steps is still a $\Theta(n^3)$ algorithm, as is $$ T_{A,2}(n) = \begin{cases} 2(n/100)^3 & \text{if }n \ne 1\,000\,000 \\ 47 & \text{if } n = 1\,000\,000\end{cases} $$ or any other number of possibilities. Both $T_{A,1}(n)$ and $T_{A,2}(n)$ satisfy $T_{A,i}(n)=2$, so they're possibilities for $T_A$, but they have different values at $1\,000\,000$, and in particular you can set $T_{A,2}(1\,000\,000)$ to be anything you like with a trivial modification.