0
$\begingroup$

This question already has an answer here:

I am working with this problem:

What is the asymptotic running time of the following piece of code?

if (N < 1000)
   for (int i = 0; i < N*N*N; i = i+1) A[i] = j;
else if (N < 10000)
   for (int i = 0; i < N; i = i+1) A[i] = j;
else
  for (int i = 0; i < N*N; i = i+1) A[i] = i;

Where the possible answers are:

  • A) linear in N
  • B) linearithmic in N
  • C) cubic N
  • D) quadratic in N

I am unsure how to calculate it, but when I look at the code I think A and B is not the right answer, because of the for-loops multiply with the variable x-times. If possible, could someone also explain what "Asymptotic time" means?

$\endgroup$

marked as duplicate by Raphael Apr 10 '16 at 16:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Asymptotic time refers to a time description neglecting constant products and sums. See also en.wikipedia.org/wiki/Big_O_notation $\endgroup$ – Andreas T Apr 10 '16 at 15:37
  • $\begingroup$ I see. But how would this codes running time be calculated? As the running depends on which if-statement is being used. If n < 1000, the forloop here is taking O(N^3 ) time to process. While the other two are slower, how do I take these to factors in the calculation? $\endgroup$ – Martin Andersen Apr 10 '16 at 15:48
0
$\begingroup$

An easy way to intuit asymptotic notations goes like this. Say you have a number $n$ and a constant $c$. Now $n^2$ will always be greater than $n*c$ for any choice of $c$ where $c < n$. So no matter what the constant coefficient is, it will never exceed $n^2$.

Now your question. Asymptotic notations are calculated for sufficiently large values of n where sufficiently large is usually taken as infinity. So although your algorithm has $n^3$ and $n$ time complexities for smaller values of $n$, for large values ($n$ tends to infinity), the time complexity is $n^2$.

$\endgroup$
0
$\begingroup$

If you analyze the pseudocode, if we make N very large, it would correspond to the else part of your code. The loop would run for $n^2$ iterations in this scenario. However, for fairly small values of N, the first two conditions are satisfied.

We are generally interested in the Worst Case Running time of a piece of code. This typically happens for very large inputs. You could argue that since the first if condition iterates the loop for $n^3$ time, the complexity would be $n^3$. However, the subtlety here is that as N gets larger and larger, for (int i = 0; i < N*N; i = i+1) A[i] = i; is dominant. Hence it would be quadratic in $n$.

A good reference for asymptotic complexity is : https://www.cs.cornell.edu/Courses/cs3110/2012sp/lectures/lec19-asymp/review.html

What's important in the definition for your code is the part which says

there exists a fixed constant $c$ and a fixed $n_0$ such that for all $n≥n_0 , f(n) ≤ cg(n).$

The $n>=n_0$ implies a bound from where the function begins to blow up. In this case, it is the larger values of N.

$\endgroup$
  • $\begingroup$ I see how it works now. "We are generally interested in the Worst Case Running time of a piece of code. This typically happens for very large inputs." This really gives sense. Thank you. $\endgroup$ – Martin Andersen Apr 10 '16 at 15:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.