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 ?
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 forn
.For Example:
So when comparing
X
andZ
, chooseX
ifn
is less thanc
, andZ
ifn
is greater thanc
. This can be repeating between the other two pairs.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:
Ignoring the constants (as you should for pure big-Oh comparison) this reduces to:
Further reduction ignoring lower order terms yields:
because the power of N
3 > 2
.Big-Oh follows a hierarchy that goes something like this:
(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