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 ofO((n^2) * log(n)), and thus the first is “better”, it is easy to see that sincelog(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 negativefandn>2)The code snap you gave is
O(nlog(n)), since the inner loop repeatslog(n)times per outer loop iteration, and the outer loop repeats n/2 times – which gives youn/2 * log(n)which is inO(nlog(n))