Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is O(n^2) greater than O((n^2)logn) [closed]

Is O(n^2) is greater than O(n^2 log n) ?
If yes ? how ?
Can we have a simple example for this.
Also ,
What is complexity of the code below.

int unknown(int n){
   int i,j,k=0;
   for(i=n/2;i<=n;i++){
     for(j=2;j<=n;j=j * 2){
         k =k + n/2;
     }
  }
return k;
}

and What is complexity of return value k ?

like image 798
Nagesh Salunke Avatar asked Dec 04 '22 00:12

Nagesh Salunke


1 Answers

O(n^2) is a subset of O((n^2) * log(n)), and thus the first is "better", it is easy to see that since log(n) is an increasing function, by multiplying something with it, you get a "higher" function then the original (f(n) <= f(n) * log(n) for each increasing non negative f and n>2)

The code snap you gave is O(nlog(n)), since the inner loop repeats log(n) times per outer loop iteration, and the outer loop repeats n/2 times - which gives you n/2 * log(n) which is in O(nlog(n))

like image 110
amit Avatar answered Dec 21 '22 23:12

amit