简体   繁体   English

算法分析:选择最佳算法

[英]Algorithm analysis: Choosing the best algorithm

I'm trying to solve a question about comparing 2 algorithms in terms of their worst case running times and finding the input size for which one algorithm would have a faster running time than the other. 我正在尝试解决一个问题,即根据最坏情况的运行时间比较两种算法,并找到一种算法比另一种算法运行时间更快的输入大小。

The two algorithms are: 这两种算法是:
A 1 = 2n log 10 n A 1 = 2n对数10 n
A 2 = 0.1n 2 A 2 = 0.1n 2

Basically, I am trying to solve the following inequality for n: 基本上,我试图解决n的以下不等式:
2n log 10 n < 0.1n 2 2n log 10 n <0.1n 2

Can anyone please point me in the right direction? 谁能指出我正确的方向?
I have managed to get up to: 我设法起床了:
log 10 n < 0.05n ==> n < 10 0.05n log 10 n <0.05n ==> n <10 0.05n

But I have no idea what to do from here (or perhaps I have gone about the wrong way trying to solve it). 但是我不知道从这里开始该怎么做(或者我已经尝试解决错误的方法了)。

Thank you for your help in advance! 提前谢谢你的帮助!

Actually, you are trying to solve the inequality 实际上,您正在尝试解决不平等问题

点1 n的平方小于2倍n对数底数10 n

because the 因为 n平方 algorithm is only going to be faster for a very short time, and then for any larger values of n, the 算法只会在很短的时间内变得更快,然后对于更大的n值, n日志n algorithm will be faster. 算法会更快。

Ignore the case n <= 0, multiply by 10, and divide by n to produce: 忽略n <= 0,乘以10并除以n的情况,得出:

n小于20 log base 10 n

Then divide by 20 and exponentiate both sides with a base of 10: 然后除以20,并以10为底对两边取幂:

10至n大于n小于20

Use a numeric solver to find the zeros of 使用数值解算器找到的零 10到n超过20减去n on the interval [1, 40] since clearly 40 is an upper bound (because 在区间[1,40]上,因为显然40是一个上限(因为 10至40等于20等于10平方等于10,040 ). )。

For instance, in Matlab: 例如,在Matlab中:

>> fzero(@(x) 10^(x/20)- x, 20)

ans =

   29.3531

So for any n an integer up through 29, the 因此,对于任何n直至29的整数, n平方 algorithm is faster, and for n > 29, the 算法更快,并且对于n> 29, n日志n algorithm wins. 算法胜出。

只需使用鼠尾草绘制功能的图像:plot(0.1 * n * n-2 * n * log(n,10),n,0,50)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM