I have the following simple C-program:
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
Now, if I take the recurence relation as
T(n) = n*T(n-1), when n>0
T(n) = 1, when n=1
then, the time complexity of this algorithm becomes O(n!), i.e., O(n^n)
and now, if I take the recurrence relation as
T(n) = T(n-1) + c, where 'c' is some constant
then, the time complexity of this algorithm becomes O(n)
Which one is correct?
I know, the time-complexity of this algorithm is O(n), but then, how can we ignore the variable 'n' which is multiplied with the function call 'factorial(n-1)'?
amultiplied byb? Do you consider it to beO(1)orO(ab)? – Adriano Aug 20 '15 at 05:37