Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better: O(n log n) or O(n^2)

Okay so I have this project I have to do, but I just don't understand it. The thing is, I have 2 algorithms. O(n^2) and O(n*log2n).

Anyway, I find out in the project info that if n<100, then O(n^2) is more efficient, but if n>=100, then O(n*log2n) is more efficient. I'm suppose to demonstrate with an example using numbers and words or drawing a photo. But the thing is, I don't understand this and I don't know how to demonstrate this.

Anyone here that can help me understand how this works?

like image 300
user3579272 Avatar asked Apr 27 '14 21:04

user3579272


People also ask

Which is better'n log n or n 2?

The only thing we can say for sure is that nlogn algorithm outperforms n2 algorithm for sufficiently large n. In practice, all nlogn algorithms have low enough multipliers that n2 algorithm can be quicker only for very small n (and for very small n, it usually doesn't matter what algorithm is used).

Is O n log n faster than O log n?

Usually the base is less than 4. So for higher values n, n*log(n) becomes greater than n. And that is why O(nlogn) > O(n).

Which time complexity is better log N or n?

The lower bound depends on the problem to be solved, not on the algorithm. Show activity on this post. Yes constant time i.e. O(1) is better than linear time O(n) because the former is not depending on the input-size of the problem. The order is O(1) > O (logn) > O (n) > O (nlogn).

Which time complexity is best?

1. O(1) has the least complexity. Often called “constant time”, if you can create an algorithm to solve the problem in O(1), you are probably at your best.


2 Answers

Good question. Actually, I always show these 3 pictures:

n = [0; 10]

enter image description here

n = [0; 100]

enter image description here

n = [0; 1000]

enter image description here

So, O(N*log(N)) is far better than O(N^2). It is much closer to O(N) than to O(N^2).

But your O(N^2) algorithm is faster for N < 100 in real life. There are a lot of reasons why it can be faster. Maybe due to better memory allocation or other "non-algorithmic" effects. Maybe O(N*log(N)) algorithm requires some data preparation phase or O(N^2) iterations are shorter. Anyway, Big-O notation is only appropriate in case of large enough Ns.

If you want to demonstrate why one algorithm is faster for small Ns, you can measure execution time of 1 iteration and constant overhead for both algorithms, then use them to correct theoretical plot:

Example

enter image description here

Or just measure execution time of both algorithms for different Ns and plot empirical data.

like image 162
Stas Avatar answered Sep 28 '22 06:09

Stas


Just ask wolframalpha if you have doubts.

In this case, it says

     n log(n) lim --------- = 0        n^2 

Or you can also calculate the limit yourself:

     n log(n)        log(n)   (Hôpital)       1/n          1 lim --------- = lim --------      =     lim ------- = lim --- = 0        n^2             n                       1           n 

That means n^2 grows faster, so n log(n) is smaller (better), when n is high enough.

like image 39
Oriol Avatar answered Sep 28 '22 08:09

Oriol