问题
Suppose that you have two algorithms (A1 and A2)
A1 = Omega(n)
A2 = O(n^2)
these algorithms determines whether a number n is a prime number or not. Which one should you choose and why?
and also when running test on a large number with A1 and A2 no difference in running time is noted. How is this possible?
回答1:
these algorithms determines whether a number n is a prime number or not. Which one should you choose and why?
The provided specification of the algorithms is not informative enough, for several reasons.
O(n^2) and Omega(n) is not informative enough to chose which has better asymptotic complexity. For example, you can have A1 runs in
Theta(n), and A2 runs inTheta(n^2), and A1 would be asymptotically better than A2. On the other hand, you could have A1 run inTheta(n^3), and A2 run inTheta(log^3(n))(which is the best known algorithm for testing primality of a number). Depending on which of these is the true complexity - the answer will varyIn addition, we don't know anything about expected input, if the expected inputs are small values of
n- big O notation (and big Omega) are not very informative, as they only handle large values.
However, the 2nd does give you some upper bound, while the first one does not, so that's a bonus. Though still not enough to be able to accurately tell which is better.
来源:https://stackoverflow.com/questions/29972024/choosing-which-algorithm-to-use