Getting time complexity using count of primitive variables

时间:2019-04-16 22:05:08

标签: loops time-complexity

I am starting to study basics of complexity analysis. As practice I want to use "count of primitive operations" to find the time complexity of the following piece of code:

int calc(int n)
{
  int i,j,res=0;

  for(i=0;i<n;i++)
  {
    for(j=0;j<100;j++)
    {
      res=res+j;
    }
  }
  return res;

}

Would the following count be correct:

+Three declarations and one assignment: 4

+For loop that iterates n times (assignment, comparison, arithmetic addition): 2(n+1)+n

+Nested For loop that iterates 100 times (it is started n times): n(2(101) + 100)

+Arithmetic addition then assignment: 2(100)

+Return: 1

Total : 4 + 2(n+1) + n + n (202+100) + 200 + 1 = 305n + 207

0 个答案:

没有答案