algorithm - 给出了三种算法的时间复杂度。对于大的 N 值,哪个应该执行最慢?

标签 algorithm data-structures time-complexity

给出的选项:

(a) O(2N)
(b) O(N)
(c) O(logN)
(d) O(n^2)

我在网上考试中遇到过这道题。他们提到的正确选项是 O(logN)。但是在我看来应该是O(2N),因为他们给出的“n^2”,“n”是小写的,否则应该是O(N^2)。有人帮我找到正确的答案。

enter image description here

最佳答案

要比较复杂度 O(A(n))O(B(n)),您应该计算极限:

lim A(n)/B(n) = ?
  n->+Inf

您可以有三种可能的结果:

   0      A (infinitely) faster than B
+Inf      A (infinitely) slower than B
   v > 0  A~B; A is v times slower than B 

在你的情况下:

lim(n^2/2n) = lim(n^2/n) = lim(n^2/log(n)) = +Inf
  n->+Inf       n->+Inf      n->+Inf

这就是为什么 (d) O(n^2)(a)..(d) 算法中最慢 的算法。

最快 算法是(c) O(log(n)),(使用L'Hospital's rule 计算极限):

lim(log(n)/2n) = lim(log(n)/n) = lim(log(n)/n^2) = 0
  n->+Inf          n->+Inf         n->+Inf

最后,算法从最快到最慢排序:

(c) O(log(n)), (b) O(n), (a) O(2*n), (d) O(n^2)

关于algorithm - 给出了三种算法的时间复杂度。对于大的 N 值,哪个应该执行最慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143065/

相关文章:

java - 如何在 Java 中实现树形数据结构?

c - 结构中定义的char占用多少字节?

javascript - 快速访问 Javascript 对象中深度嵌套的值

c - 如何在 openmp 中并行化 while 循环 - 共轭梯度

c++ - 在没有 while 和 for 循环的情况下查找根

python - 什么会影响超过 64 个字符的字符串的 Python 字符串比较性能?

java - Google Protobuf 反序列化重复对象上 get 操作的时间复杂度

c - 我的跳过列表真的是在 N 而不是 log(N) 中搜索吗?

algorithm - DNS 服务器使用什么算法来加快查找速度?

从字符串中删除所有出现的字符的代码不起作用——但为什么呢?