O(n)和O(log(n))之间的区别

编程入门 行业动态 更新时间:2024-10-21 19:39:51
本文介绍了O(n)和O(log(n))之间的区别-哪个更好,O(log(n))到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的第一门数据结构课程,每次讲座/ TA讲座,我们谈论的是 O(log(n))。这可能是一个愚蠢的问题,但如果有人可以向我确切解释这是什么意思,我将不胜感激!?

This is my first course in data structures and every lecture / TA lecture , we talk about O(log(n)) . This is probably a dumb question but I'd appreciate if someone can explain to me exactly what does it mean !?

推荐答案

它表示所讨论的事物(通常是运行时间)的缩放方式与其输入大小的对数一致。

It means that the thing in question (usually running time) scales in a manner that is consistent with the logarithm of its input size.

Big-O表示法并不表示 exact 方程,而是 bound 。例如,以下函数的输出均为O(n):

Big-O notation doesn't mean an exact equation, but rather a bound. For instance, the output of the following functions is all O(n):

f(x) = 3x g(x) = 0.5x m(x) = x + 5

因为随着增加x,它们的输出全部呈线性增加-如果 f(n)和 g(n), f(10 * n)和 g(10 * n)依此类推。

Because as you increase x, their outputs all increase linearly - if there's a 6:1 ratio between f(n) and g(n), there will also be approximately a 6:1 ratio between f(10*n) and g(10*n) and so on.

关于 O(n)或 O(log n)更好,请考虑:如果 n = 1000 ,则 log n = 3 (对于log-base-10)。您宁愿算法运行1000秒还是3秒?

As for whether O(n) or O(log n) is better, consider: if n = 1000, then log n = 3 (for log-base-10). Which would you rather have your algorithm take to run: 1000 seconds, or 3 seconds?

更多推荐

O(n)和O(log(n))之间的区别

本文发布于:2023-11-29 16:33:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   log

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!