O(n) 算法的计算时间可以超过 O(n^2) 吗?

2024-01-07

假设我有两种算法:

for (int i = 0; i < n; i++) {
  for (int j = 0; j < n; j++) {
    //do something in constant time
  }
}

这自然是O(n^2)。假设我也有:

for (int i = 0; i < 100; i++) {
  for (int j = 0; j < n; j++) {
    //do something in constant time
  }
}

This is O(n) + O(n) + O(n) + O(n) + ... O(n) + = O(n)

看来即使我的第二个算法是O(n),需要更长的时间。有人可以对此进行扩展吗?我提出它是因为我经常看到算法,例如,它们首先执行排序步骤或类似的操作,并且在确定总复杂度时,它只是限制算法的最复杂元素。


渐近复杂度(big-O 和 big-Theta 都代表的意思)完全忽略了所涉及的常数因素 - 它只是为了表明随着输入大小变大,运行时间将如何变化。

So it's certainly possible that an Θ(n) algorithm can take longer than an Θ(n2) one for some given n - which n this will happen for will really depend on the algorithms involved - for your specific example, this will be the case for n < 100, ignoring the possibility of optimizations differing between the two.

For any two given algorithms taking Θ(n) and Θ(n2) time respectively, what you're likely to see is that either:

  • The Θ(n) algorithm is slower when n is small, then the Θ(n2) one becomes slower as n increases
    (which happens if the Θ(n) one is more complex, i.e. has higher constant factors), or
  • The Θ(n2) one is always slower.

Although it's certainly possible that the Θ(n) algorithm can be slower, then the Θ(n2) one, then the Θ(n) one again, and so on as n increases, until n gets very large, from which point onwards the Θ(n2) one will always be slower, although it's greatly unlikely to happen.

用更数学化的术语来说:

Let's say the Θ(n2) algorithm takes cn2 operations for some c.

And the Θ(n)算法需要dn一些操作d.

这符合正式定义 http://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachmann.E2.80.93Landau_notations因为我们可以假设这适用于n大于 0(即对于所有n)并且运行时间所在的两个函数是相同的。

In line with your example, if you were to say c = 1 and d = 100, then the Θ(n) algorithm would be slower until n = 100, at which point the Θ(n2) algorithm would become slower.

(courtesy of WolframAlpha https://www.wolframalpha.com/input/?i=n%5E2+vs+100n+from+0+to+130).

注释:

Technically big-O is only an upper bound, meaning you can say an O(1) algorithm (or really any algorithm taking O(n2) or less time) takes O(n2) as well. Thus I instead used big-Theta (Θ) notation, which is just a tight bound. See the formal definitions http://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachmann.E2.80.93Landau_notations for more information.

Big-O 通常被非正式地视为或教导为紧界,因此您可能已经在不知情的情况下本质上使用了 big-Theta。

If we're talking about an upper bound only (as per the formal definition of big-O), that would rather be an "anything goes" situation - the O(n) one can be faster, the O(n2) one can be faster or they can take the same amount of time (asymptotically) - one usually can't make particularly meaningful conclusions with regard to comparing the big-O of algorithms, one can only say that, given a big-O of some algorithm, that that algorithm won't take any longer than that amount of time (asymptotically).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

O(n) 算法的计算时间可以超过 O(n^2) 吗? 的相关文章

  • for 循环的增长顺序复杂

    对于以下代码片段 N 的增长顺序是多少 int sum 0 for int i 1 i lt N i i 2 for int j 1 j lt N j j 2 for int k 1 k lt i k sum 我发现有 lgN 项 但我一直
  • TreeMap - 搜索时间复杂度

    TreeMap 中 get 和 put 的时间复杂度是多少 实现方式和红黑树一样吗 从这里 http java sun com javase 6 docs api java util TreeMap html http java sun c
  • Dictionary.Keys 返回的 KeyCollection 操作有多快? (。网)

    IDictionary
  • 为什么冒泡排序最好情况的时间复杂度是O(n)

    我按照书中使用的方法推导了冒泡排序在最佳情况下的时间复杂度算法2 2 但结果是 O n 2 以下是我的推导 希望大家帮我找出哪里错了 public void bubbleSort int arr for int i 0 len arr le
  • unordered_set::find 的复杂性可以预测吗?

    在寻找适合我正在构建的应用程序的容器时 我浏览了以下文档unordered set 鉴于我的应用程序通常只需要insert and find函数 这个类看起来相当有吸引力 然而 我有点推迟了 因为find是 O 1 摊销 但最坏情况是 O
  • 深度优先图算法的时间复杂度[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我开始学习时间复杂度 并且我在示例中查找了一些简单排序的时间复杂度 我想知道如何计算图中深度优先搜索的平均时间复杂度 V n and E
  • 算法时间复杂度分析

    您好 我正在尝试分析该算法的时间复杂度 但我很难解开并计算最终循环将执行的次数 我意识到第一个循环是 log n 但之后我似乎无法得到一个评估良好的总和 这是算法 for int i 1 i lt n i 2 i for int j 1 j
  • 教科书上的长除法如何是 O(n^2) 算法?

    Premise This 维基百科页面 http en wikipedia org wiki Computational complexity of mathematical operations建议 的计算复杂度 教科书 长除法 http
  • 两次调用的递归函数的时间复杂度

    考虑这段代码 def count 7 lst if len lst 1 if lst 0 7 return 1 else return 0 return count 7 lst len lst 2 count 7 lst len lst 2
  • 查找数组长度的时间复杂度

    我对时间复杂度有点困惑len 函数将是 我读过很多不同的文章 在 python 中查找数组的长度是O 1 与len 函数和其他语言类似 这怎么可能 您是否不必遍历整个数组来计算它占用了多少个索引 您是否不必遍历整个数组来计算它占用了多少个索
  • 时间复杂度单循环与多个顺序循环

    今天 我和我的同事就一个特定的代码片段发生了一场小争论 代码看起来像这样 至少 他想象中是这样的 for int i 0 i lt n i Some operations here for int i 0 i lt m i m is alw
  • 非不相交集并集的最佳算法是什么?

    假设有两个 非不相交 点集 笛卡尔空间 执行这两个集的并集的最佳情况复杂度算法是什么 由于点坐标是任意的 并且它们之间没有特殊关系 因此我不认为这个问题是一个几何特定问题 这是有效地将 S1 和 S2 合并成新集合 S 的通用问题 我知道有
  • 这段代码的复杂度是多少? (大O)这是线性的吗?

    for int i 0 i
  • Google Chrome 中 array.splice() 的时间复杂度是多少?

    如果我使用 splice 从数组中删除一个元素 如下所示 arr splice i 1 这会是O n 在最坏的情况下 因为它会移动 i 之后的所有元素 或者它是常数时间 下面有一些链表魔法 最坏的情况下should be O n 复制所有n
  • 找出数组中重复的元素

    有一个大小为 n 的数组 数组中包含的元素在 1 到 n 1 之间 每个元素出现一次 只有一个元素出现多次 我们需要找到这个元素 尽管这是一个非常常见的常见问题解答 但我仍然没有找到正确的答案 大多数建议是我应该将数组中的所有元素相加 然后
  • Dijkstra算法的时间复杂度是多少

    Dijkstra V E S O 1 for each vertex v V O V d v O 1 d source 0 O 1 while S V O V v non visited vertex with the smallest d
  • Big O 用于有限、固定大小的可能值集

    这个问题 https stackoverflow com questions 12305028 java what is the best way to find first duplicate character in a string引
  • 除法的时间复杂度是多少?

    我使用除法算法 根据https en wikipedia org wiki Computational complexity of mathematical operations https en wikipedia org wiki Co
  • 两个非嵌套循环的大 O 表示法

    对于两个非嵌套的 for 循环 大 O 表示法是什么 Example for int i 0 i
  • 递归和大O

    我最近正在完成一项涉及递归和大 O 表示法的计算机科学作业 我相信我很好地理解了这一点 虽然当然不是完美的 但是有一个问题给我带来了最多的问题 奇怪的是 一看就知道是作业上最简单的一个 使用大哦符号提供最佳增长率来解决以下递归问题 T 1

随机推荐

  • 将 PowerShell 变量传递到脚本块

    我正在尝试获取 PowerShell 变量并将它们应用到脚本块 param string username throw Blackberry Admin User Name is required string password throw
  • PostgreSQL 使用 tf-idf 吗?

    我想知道 PostgreSQL 9 3 中使用 GIN GiST 索引的全文搜索是否使用 tf idf 术语频率 逆文档频率 特别是 在我的短语列中 我有一些更受欢迎的单词 而有些则非常独特 即名称 我想对这些列建立索引 以便匹配的唯一单词
  • django.db.utils.InternalError:(1050,“表'django_content_type'已经存在”)

    django db utils InternalError 1050 表 django content type 已经存在 我刚刚从我的朋友那里复制了一个项目 当我运行 makemirations 时它运行正常 但对于 python3 ma
  • 将元素添加到对象数组中

    我有一堂课叫receipt其中一个属性是一个数组item items 我有一个方法addItem string name int quantity double price 我的问题是如何将这些参数添加到数组中items 那么如何检查数量是
  • jQuery 从 Google 标签管理器加载到 gtm.js

    我遇到了一个问题 jQuery v1 9 1 被包含在 gtm js 文件的顶部 它会导致一些问题 并且可能会破坏已经加载到 jQuery fn 上的 jQuery 插件 回归测试也是一个问题 我检查了一下 在加载 jQuery 的 Goo
  • SQL*Plus :强制它返回错误代码

    我有一个存储过程 它有一个 OUT 参数 指示错误代码 如果错误代码不为 0 那么我会提出错误 DECLARE BEGIN foo err code IF err code lt gt 0 THEN raise application er
  • LINQ-to-Entities(不是 Linq-to-SQL)中是否有 DataContext?

    我最近问了一个关于跟踪 Linq to Entity 的问题 https stackoverflow com questions 137712 sql tracing linq to entities 我觉得答案之一 https stack
  • 当表中不存在列时如何将 paginate() 与having() 子句一起使用

    我有一个棘手的案例 以下数据库查询不起作用 DB table posts gt select posts DB raw haversineSQL as distance gt having distance lt distance gt p
  • Java 8 中多重继承的用法

    Am I usingJava 8 的一个功能或misusing it 请参阅下面的代码和解释以了解为什么选择这样 public interface Drawable public void compileProgram public Pro
  • 线程编程中的守护简单列表?

    我正在阅读一本 POSIX 线程书籍进行一些练习 并且我试图找出在一个简单的单链表中需要互斥锁的位置作为一个小练习问题 例如 如果我有一个节点结构列表 template
  • PHP - 使用 LOAD DATA INFILE 将 CSV 文件导入到 mysql 数据库

    我有一个这样的 csv 文件数据 Date Name Call Type Number Duration Address PostalCode City State Country Latitude Longitude Sep 18 201
  • 使用unicode字符u201c

    我是 python 新手 在理解 unicode 时遇到问题 我在用着 Python 3 4 我花了一整天的时间试图通过阅读有关 unicode 的内容来解决这个问题 包括http www fileformat info info unic
  • 具有新 Firebase 的 Nodejs 应用程序不会检索数据库项目

    我是 Nodejs 新手 但已经有一个工作的 js 客户端程序 Firebase 版本 3 0 2 事实证明 我需要一个服务器来完成一些在 js 客户端中不可能完成的简单事情 当我在 Nodejs 中尝试这个基本的事情时 没有任何反应 数据
  • 在 MATLAB 中查找变量的小数位数

    给定变量 x 12 3442 我想知道变量的小数位数 在这种情况下 结果将是 4 如何在不反复试验的情况下做到这一点 这是一个紧凑的方法 y x 10 1 20 find y round y 1 假设x是您的数字 20 是小数点后的最大位数
  • 在java中将数组的字符串表示形式转换回int数组

    刚刚开始使用 Java 编程 如果我有一个存储在 txt 文件中的数组 如下所示 10 22 30 55 10 20 19 如何将其转换回正常的 int 数组以在代码中使用 我需要能够将其简单地存储在这样的 txt 文件中 以便我可以手动对
  • 如何在 Windows 上安装 python-levenshtein?

    经过几天的搜索 我准备放弃寻找 Python 2 7 Windows 64 位 的预编译二进制文件Python Levenshtein 库 http pypi python org pypi python Levenshtein 所以不是我
  • Java 中的 getter/setter

    我是 Java 新手 但有一些使用 ActionScript 3 的 OOP 经验 因此我尝试依靠我所知道的内容进行迁移 在 ActionScript 3 中 您可以使用 get 和 set 关键字创建 getter 和 setter 这意
  • 相机控件在 iOS 7 上不可见

    我使用图像选择器控制器来调用设备相机 下面列出的代码在 iOS 7 下工作正常 但是当我在 iOS 7 上使用相同的代码启动相机时 我看不到 使用 和 取消 按钮 void getCameraPicture UIImagePickerCon
  • R - 使用“rep”创建重复序列

    我想知道是否有更简单的方法来制作列表 例如 10 4 20 6 和 30 3 然后手写 example lt c 4 4 4 4 与函数 rep 我知道我可以重复某个序列 n 次 每次重复 n 次 但我不知道如何用每个数字的不同数量来制作一
  • O(n) 算法的计算时间可以超过 O(n^2) 吗?

    假设我有两种算法 for int i 0 i lt n i for int j 0 j lt n j do something in constant time 这自然是O n 2 假设我也有 for int i 0 i lt 100 i