algorithm - 简单算法的大 O 符号表示

标签 algorithm big-o computer-science

如何使用大 O 表示法表示其复杂性?我有点困惑,因为第二个 for 循环根据外循环的索引而变化。还是O(n^2)吗?还是不那么复杂?提前致谢

for (int k = 0; k<arr.length; k++){
      for (m = k; m<arr.length; m++){
           //do something
      }
}

最佳答案

您的估计来自progression公式:

enter image description here

因此,是 O(n^2)。为什么你的情况是进展?因为它是循环的 n + (n-1) + ... + 1 总和。

关于algorithm - 简单算法的大 O 符号表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949554/

相关文章:

math - 十六进制数的前导字符 (x0)

javascript - 字符串相乘 - [Leetcode] 与 JavaScript

python - 还记得上次函数调用的结果吗?

c# - 二维数组到一维数组 C#

c++ - 将 bool 数组转换为 int32、unsigned int 和 double?

algorithm - 时间复杂度 - O(n^2) 到 O(n log n) 搜索

computer-science - 第一个 NP 完全问题是如何被证明是 NP 完全的?

algorithm - 比较大 O 表示法

algorithm - 为什么通过插入元素构建堆的运行时间比使用 heapify 差?

computer-science - 根据指令大小、操作码大小和操作数数量查找寄存器计数的最大数量