Compare Big O Notation

2019-08-17 18:51发布

问题:

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.