计算程序运行时间?

发布于 2025-01-07 04:51:58 字数 408 浏览 1 评论 0原文

我正在尝试计算我的分割程序的运行时间,

void splitX(int x) {
     if (x<=1) {return x;};
     splitX(n/2);
     System.out.println("splitting in progress");
     splitX(n/2);
     splitX(n/2);
     splitX(n/2);
}

我对此相当陌生,这就是到目前为止所做的;

T(n) = 4T(n/2)
     = 4^2T(n/2^2)
     = 4^3T(n/2^3)
     = 4^kT(n/2^k)
     = O(log n)

我走在正确的轨道上吗,我有点困惑,你还必须考虑打印线吗?

I am trying to work out the running time of my splitting program,

void splitX(int x) {
     if (x<=1) {return x;};
     splitX(n/2);
     System.out.println("splitting in progress");
     splitX(n/2);
     splitX(n/2);
     splitX(n/2);
}

I am fairly new to this, this is what have done so far;

T(n) = 4T(n/2)
     = 4^2T(n/2^2)
     = 4^3T(n/2^3)
     = 4^kT(n/2^k)
     = O(log n)

Am i on the right track, im getting a little confused, also do you have to account for the printing line?

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

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

发布评论

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

评论(4

蘸点软妹酱 2025-01-14 04:51:58

分析到最后都是正确的,解为 T(n) = O(n^2)

需要注意的是 4^kT(n/2^k) != O(log n ) 因为 4^k 不是常量。

看一下分析:

T(n) = 4T(n/2) = 
     = 4^2T(n/2^2)
     = 4^3T(n/2^3)
     = 4^kT(n/2^k)
     = 4^log(n)*T(1) =
     = 4^log(n) * 1 =
     = (2^log(n))^2 =
     = n^2
     = O(n^2)

为了正式证明它:我们使用归纳

基数:T(1) = 1 = 1^2

假设每个 k <= n 都为 T(n) = n^2

T(2n) = 4*T(n) =(归纳假设) 4*n^2 = (2n)^2

因此归纳假设为真,且 T(n ) = n^2

您还可以在 wolfram alpha

The analyzis is true until the end, the solution is T(n) = O(n^2)

Note that 4^kT(n/2^k) != O(log n) since 4^k is not a constant.

Have a look at the analyzis:

T(n) = 4T(n/2) = 
     = 4^2T(n/2^2)
     = 4^3T(n/2^3)
     = 4^kT(n/2^k)
     = 4^log(n)*T(1) =
     = 4^log(n) * 1 =
     = (2^log(n))^2 =
     = n^2
     = O(n^2)

To formally prove it: we use induction

base: T(1) = 1 = 1^2

Assume T(n) = n^2 for each k <= n

T(2n) = 4*T(n) =(induction hypothesis) 4*n^2 = (2n)^2

Thus the induction hypothesis is true and T(n) = n^2

You can also check this result on wolfram alpha

束缚m 2025-01-14 04:51:58

正如我所见,您对递归函数进行了 log(N) 次调用。将其乘以常数 - 4- 不会改变复杂性,打印行也不会改变(满足所有作业相关的需求)。

As I see it you make log(N) calls to your recursive function. multiplying this by a constant - 4- does not change complexity, nor does the printing line (for all homework related needs).

会傲 2025-01-14 04:51:58

是的,用 Big-O 表示法来说,它是 O(log n) 乘以常数。

Yes, in Big-O notation it's O(log n) mulltiply by constant.

恬淡成诗 2025-01-14 04:51:58

表达式的形式为

T(n)=4T(n/2) + c

现在使用 a=4、b=2 和 f(n)=c 应用主定理;

T(n)=O(n^loga) //底数2

T(n)=n^2

the expression is of form

T(n)=4T(n/2) + c

now apply master theorum using a=4, b=2 and f(n)=c;

T(n)=O(n^loga) //base 2

T(n)=n^2

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