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

前端 未结 6 753
忘掉有多难
忘掉有多难 2020-12-22 18:43

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)

6条回答
  •  眼角桃花
    2020-12-22 19:38

    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.

提交回复
热议问题