-1

Can I get help to give an analysis of the running time Big-O? I'm not sure if all my answers are correct.

I got for a) $ O(n)$, b) $O(n^3)$, c) $O(n^{1/2})$ and d) $O(log(n))$

(a)                             
   sum=0;
   for ( i =0; i<n ; i +=10)          
      sum++;


(b) 
    sum=0;
    for ( i =0; i<n*n;++ i )
        for ( j =n ; j >0;--j )
            ++sum;

(c)
    sum=0;
    for ( i =0 , j =n ; i<j ;++ i , j --)
        ++sum;

(d)
    int function (long n){
       int sum = 0 ;
       for (;n>0;n/=2){
        sum+=n%2;
       }
       return sum;
    }
Kyle Jones
  • 8,207
  • 2
  • 30
  • 52
dfram
  • 11
  • 1

2 Answers2

2

c) is incorrect.

It looks like you are saying it is $O(n^{\frac{1}{2}})$ or $O(\sqrt{n})$. That cannot be the case. You are adding one to $i$ and subtracting one from $j$ on each loop iteration. That means the counters should meet in the middle.

mikeazo
  • 233
  • 1
  • 9
1

As you said:

a)running n/10 times therefore O(n)

b)outer loop n^2 times inner loop n times hence O(n^3)

c)Runs n/2-1 times :hence O(n)

d)Runs n/(2^k)=1 where k is some integer(no of times loop runs) on solving we get k=O(logn) , in base 2.