O(n^2) vs O (n(logn)^2)

后端 未结 6 2129
悲哀的现实
悲哀的现实 2021-02-01 23:18

Is time complexity O(n^2) or O (n(logn)^2) better?

I know that when we simplify it, it becomes

O(n) vs O((logn)^2)
6条回答
  •  深忆病人
    2021-02-01 23:49

    n is only less than (log n)2 for values of n less than 0.49...

    So in general (log n)2 is better for large n...

    But since these O(something)-notations always leave out constant factors, in your case it might not be possible to say for sure which algorithm is better...

    Here's a graph:

    enter image description here

    (The blue line is n and the green line is (log n)2)

    Notice, how the difference for small values of n isn't so big and might easily be dwarfed by the constant factors not included in the Big-O notation.

    But for large n, (log n)2 wins hands down:

    enter image description here

提交回复
热议问题