Choosing which algorithm to use

我怕爱的太早我们不能终老 提交于 2020-01-06 20:25:41

问题


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.

  1. 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 in Theta(n^2), and A1 would be asymptotically better than A2. On the other hand, you could have A1 run in Theta(n^3), and A2 run in Theta(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 vary

  2. In 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

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