关于 while 循环在 for 循环中的时间复杂度的混淆

发布于 2025-01-11 13:38:02 字数 1697 浏览 5 评论 0原文

我正在学习时间复杂度,并且理解它的要点,但有一件事真正让我困惑,那就是理解 for 循环中 while 循环的时间复杂度。

这是我正在分析的代码:

    sum := 0
    for i := 1 to n
       j := 1
       while j ≤ i
          sum += j
          j*=5
       end
    end

我已经对此进行了尝试,并制作了这个表格,将其分解:

代码:成本:# OF TIMES:时间复杂度:
sum := 0111
for i := 1 到 nint i = 1 (1)12n+1
i<=n (1)n+1
i++ (1)n
j := 11nn
当 j ≤ ij ≤ i (1)
sum += j1?
j*=51
end010
end010

我理解 for 循环的时间复杂度是如何工作的,但是当我到达 while 循环时我很困惑。

我知道赋值的成本为 1,比较的成本为 1。

如果 while 循环写为:

    sum:=0
    j:=1
    while j<=n
       sum+=j
       j*=5
    end

由于它以 5 的增量移动:(j*=5),其时间复杂度为为:log base5 n

但是代码中的 while 循环出现了 j<=i,这让我很困惑。

有人可以帮助我计算 while 循环各行的成本/次数,我真的很感激。

仅供参考:这不是学校的作业或类似的东西,我真诚地试图为自己理解这个概念。

如果上面的表格格式不正确,这里有一个内容

I'm learning about time complexity and I understand the gist of it, but there's one thing that's really confusing me, it's about understanding the time complexity of a while loop in a for loop.

This is the code I am analyzing:

    sum := 0
    for i := 1 to n
       j := 1
       while j ≤ i
          sum += j
          j*=5
       end
    end

I've given a shot at this and I made this table, breaking it down:

CODE:COST:# OF TIMES:TIME COMPLEXITY:
sum := 0111
for i := 1 to nint i = 1 (1)12n+1
i<=n (1)n+1
i++ (1)n
j := 11nn
while j ≤ ij ≤ i (1)??
sum += j1??
j*=51??
end010
end010

I understand the how the time complexity works for the for loop, but when I get to the while loop I'm confused.

I know that assignments have cost of 1 and comparisons have a cost of 1.

If the while loop was written as:

    sum:=0
    j:=1
    while j<=n
       sum+=j
       j*=5
    end

Since it's moving in increments of 5: (j*=5), its time complexity would be: log base5 n.

But the while loop in the code goes j<=i, which is throwing me off.

I someone could help me with cost/# of times the individual lines of the while loop, I would really appreciate it.

fyi: this isn't an assignment for school or anything like that, I'm genuinely trying to understand this concept for myself.

If the table above doesn't format correctly, here is a ss of it

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

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

发布评论

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

评论(1

蓬勃野心 2025-01-18 13:38:02

您的循环相当于从 1n 的所有 ilog_5(i) 之和。对数底数并不重要,它是一个常数;所以从现在开始我只会说log。该总和渐近等于n log n。这样想:如果您查看 log 图表,它真的非常平坦,因此您的值中的大部分求和看起来与 log 函数相同。这只是一个直觉提示,不是正式证明,但已经足够了。如果您需要正式证明,查看这篇文章

Your loop is equivalent to the sum of log_5(i) for all i from 1 to n. The logarithm base doesn't matter, it's a constant; so i'll just say log from now on. This summation is asymptotically equal to n log n. Think about it this way: If you look at the graph of log, it's really, really flat, so most of the higher values in your summation look the same to the log function. This is only an intuition hint, not a formal proof, but it's sufficient. If you need a formal proof, check out this post

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