大O表示法 用于访问链表和二分查找中的中间元素?

发布于 2024-11-28 05:54:03 字数 433 浏览 1 评论 0原文

文章位于 http://leepoint.net/notes-java/algorithms/big -oh/bigoh.html 表示访问链表中的中间元素的大 O 表示法是 O(N) 。不应该是 O(N/2) 。假设我们在链表中有 100 个元素所以访问第 50 个元素必须从第一个节点向右遍历到第 50 个。因此操作次数将在 50 左右。

为二分查找定义的另一大符号是 log(N)。假设我们有 1000 个元素。根据 log(N),我们将需要接近 3 的运算。现在让我们手动计算,下面将是模式

第 500 个元素、第 250、125、63、32、16、8、4、2,因此运算约为 9它比 3 大得多。

我在这里缺少什么吗?

Article at http://leepoint.net/notes-java/algorithms/big-oh/bigoh.html says that Big O notation For accessing middle element in linked list is O(N) .should not it be O(N/2) .Assume we have 100 elements in linked list So to access the 50th element it has to traverse right from first node to 50th. So number of operation will be around 50.

One more big notation defined for binary search is log(N). Assume we have 1000 elements. As per log(N) we will be needing the opeartion close to 3. Now lets calculate it manually below will be the pattern

500 th element, 250th, 125,63,32,16,8,4,2 so operation are around 9 which is much larger than 3.

Is there any thing i am missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜心小果奶 2024-12-05 05:54:04

O(n) 表示“对于所有较大的 n,与 n比例”。1

所以它不必等于n

编辑:

我上面的解释有点不清楚 O(n) 中的某些内容是否也在 O(n2) 中。它 - 我对“比例”这个词的使用很差,正如其他人试图提到的那样(正如我最初的误解一样),“收敛”将是一个更好的术语。2

1:它实际上意味着“在最坏的情况下,当n很大时,与n成比例-案例场景”,但书籍通常会考虑“平均”情况,更准确地表示为 f(n) ~ n,其中 f 是您的函数。

2:更多技术人员:这应该是显而易见的,但我打算在数学上严谨。如果您正在寻找形式证明(带有比率、限制等),Math.SE 可能是提出这个问题的更好选择。

O(n) means "proportional to n, for all large n".1

So it doesn't have to be equal to n.

Edit:

My explanation above was a little unclear as to whether something in O(n) is also in O(n2). It is -- my use of the word "proportional" was poor, and as the others were trying to mention (and as I was misunderstanding originally), "converge" would be a better term.2

1: It actually means "proportional to n when n is large in the worst-case scenario", but books usually consider the "average" case, which is more accurately represented by f(n) ~ n, where f is your function.

2: For the more technical people: it should be obvious, but I am not intending to be mathematically rigorous. Math.SE might be a better choice for asking this question, if you're looking for formal proofs (with ratios, limits, and whatnot).

童话里做英雄 2024-12-05 05:54:04

从您链接的网页:

同样,常量乘数也被忽略。所以 O(4*N) 算法是
等价于 O(N),这就是它应该写的。最终你
在确定时要注意这些乘数
性能,但对于使用 Big-Oh 的第一轮分析,您
简单地忽略常数因素。

From the web page you linked:

Similarly, constant multipliers are ignored. So a O(4*N) algorithm is
equivalent to O(N), which is how it should be written. Ultimately you
want to pay attention to these multipliers in determining the
performance, but for the first round of analysis using Big-Oh, you
simply ignore constant factors.

無處可尋 2024-12-05 05:54:04

Big O 表示法是算法效率的通用表达。您不应将其解释为算法速度或需要多少内存的精确方程,而应将其视为近似值。函数的常数乘数将被忽略,因此例如您可以将示例视为 O 1/2(N) 或 O k(N),删除 k这给你 O(N)。

The Big O notation is a general expression of an algorithm's efficiency. You should not construe it as an exact equation for how fast an algorithm is going to be, or how much memory it is going to require, but rather view it as an approximation. The constant multipliers of the function are ignored, so for example you could view your example as O 1/2(N), or O k(N), drop the k which gives you O(N).

栖竹 2024-12-05 05:54:03

你缺少的是任何常数倍数对于 Big O 来说都不重要。所以我们有 O(N) = O(N/2)。

关于问题的 log 部分,它实际上是 log2(N) 而不是 log10(N),所以在这种情况下 log(1000) 实际上是 9 (当向下舍入)。另外,和以前一样,O(log2(N)) = O(log10(N)),因为两者只是彼此的常量倍数。具体来说,log2(N) = log10(N) / log10(2)

最后要考虑的是,如果有几个函数相加在一起时,低阶函数与 Big O 无关。这是因为高阶函数比低阶函数增长得更快。所以我们发现 O(N3 + N) = O(N3) 和 O(eN + N 2 + 1) = O(eN)。

因此需要考虑两件事:删除倍数和删除低次函数。

What you're is missing that any constant multiples don't matter for Big O. So we have O(N) = O(N/2).

About the log part of the question, it is actually log2(N) not log10(N), so in this case log(1000) is actually 9 (when rounding down). Also, as before, O(log2(N)) = O(log10(N)), since the two are just constant multiples of each other. Specifically, log2(N) = log10(N) / log10(2)

The last thing to consider is that, if several functions are added together, the function of lower degree doesn't matter with Big O. That's because higher degree functions grow more quickly than functions of lower degree. So we find things like O(N3 + N) = O(N3), and O(eN + N2 + 1) = O(eN).

So that's two things to consider: drop multiples, and drop function of low degree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文