Search code examples
algorithmtime-complexitybig-ocomplexity-theory

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?


Solution

  • 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.