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 ?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With