Compare Big O Notation

故事扮演 提交于 2019-12-12 01:16:24

问题


In n-element array sorting processing takes;
in X algorithm: 10-8n2 sec,
in Y algoritm 10-6n log2n sec,
in Z algoritm 10-5 sec.

My question is how do i compare them. For example for y works faster according to x, Which should I choose the number of elements ?


回答1:


When comparing Big-Oh notations, you ignore all constants:

N^2 has a higher growth rate than N*log(N) which still grows more quickly than O(1) [constant].

The power of N determines the growth rate.

Example:

O(n^3 + 2n + 10) > O(200n^2 + 1000n + 5000)

Ignoring the constants (as you should for pure big-Oh comparison) this reduces to:

O(n^3 + n) > O(n^2 + n)

Further reduction ignoring lower order terms yields:

O(n^3) > O(n^2)

because the power of N 3 > 2.

Big-Oh follows a hierarchy that goes something like this:

O(1) < O(log[n]) < O(n) < O(n*log[n]) < O(n^x) < O(x^n) < O(n!)

(Where x is any amount greater than 1, even the tiniest bit.)

You can compare any other expression in terms of n via some rules which I will not post here, but should be looked up in Wikipedia. I list O(n*log[n]) because it is rather common in sorting algorithms; for details regarding logarithms with different bases or different powers, check a reference source (did I mention Wikipedia?)

Give the wiki article a shot: http://en.wikipedia.org/wiki/Big_O_notation




回答2:


I propose this different solution since there is not an accepted answer yet.

If you want to see at what value of n does one algorithm perform better than another, you should set the algorthim times equal to each other and solve for n.

For Example:

X = Z  
10^-8 n^2 = 10^-5  
n^2 = 10^3  
n = sqrt(10^3)
let c = sqrt(10^3)

So when comparing X and Z, choose X if n is less than c, and Z if n is greater than c. This can be repeating between the other two pairs.



来源:https://stackoverflow.com/questions/11940730/compare-big-o-notation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!