Algorithm 确定这些不同循环的big-O运行时?
我有一系列的问题需要反馈和回答。我会评论一下我的想法,这不是家庭作业,而是为考试做准备 我的主要问题是确定不同情况下循环的迭代次数。我们该如何去尝试找出答案呢 评估运行时间 问题2 我的答覆是:O(n) b) 为简单起见,假设n=3^kAlgorithm 确定这些不同循环的big-O运行时?,algorithm,math,big-o,time-complexity,Algorithm,Math,Big O,Time Complexity,我有一系列的问题需要反馈和回答。我会评论一下我的想法,这不是家庭作业,而是为考试做准备 我的主要问题是确定不同情况下循环的迭代次数。我们该如何去尝试找出答案呢 评估运行时间 问题2 我的答覆是:O(n) b) 为简单起见,假设n=3^k int z=0; int x=0; for (int i=1; i<=n; i=i*3){ // runs n/3 times? how does it effect final answer? z = z+5;
int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){ // runs n/3 times? how does it effect final answer?
z = z+5;
z++;
x = 2*x;
}
intz=0;
int x=0;
对于(inti=1;i让我们一次看一个
第(一)部分
我的答覆是:O(n)
是的!这是正确的。循环运行O(n)次,每次迭代做O(1)个工作
第(二)部分
我的答案是:O(logn)
不完全一样。请注意,j
仍在线性增长,但循环的运行时间与j2一样长≤ n、 这意味着一旦j超过√ n、 循环将停止。因此,只有O(√n) 循环的迭代,因为每个循环都做O(1)个功,所以完成的总功是O(√n)
第(四)部分
我的答案是:n x logn x logn=O(n logn^2)
不完全是这样。看第二个循环。它实际上运行O(√n) 第三个内部循环也运行O(√n) 次,因此所做的总功为O(n2)
第(i)部
inta=0;
int k=n*n;
而(k>1)//运行n^2
{
对于(int j=0;jDid您是否查阅过一本书?其中一些是最基本的分析案例。当您将一个内循环的运行时间计算为n^2时,您已经考虑了外循环的时间效率。因此,您不会在问题结束时将其乘以外循环的效率……这是极端的重复计算。一个很好的通用方法假设有三个嵌套循环分别运行i、j和k次,则效率为O(ijk)。如果考虑到这一点,使用相同的逻辑,两个嵌套循环分别运行n次,则得到O(n*n)=O(n^2)。对于n-1情况等,可以将其估计为n——大O是一个上界q2)wtf?i=i++
和i*i,j++
(逗号)for(int i=0;i<=n;i++)//运行n次
错误:运行n+1次不应将(f)分为O(n^2),因为外循环运行n次,内循环运行n,因为0+n+2n+3n+…+nx(x+1)/2=n(1+3+……x(x+1)/2),所以总数是O(n^2)在我看来,内循环的第一次迭代运行0次,第二次运行n次,第三次运行2n次,等等。这意味着完成的总功是0+n+2n+3n+…+(n-1)n=n(0+1+2+…+n-1)。这是n(n-1)n/2=Theta(n^3).我的推理有什么问题吗?它看起来就像是0+n+2n+..是一个数学级数,与我上面发布的结果相等..我不确定(n-1)n?0+1+2+…+n-1是n(n-1)/2,所以0n+1n+2n+…+(n-1)n=(0+1+2+…+n-1)n=n(n-1)n/2。这不正确吗?另外,我认为你的系列是错误的-最后一项应该是n(n-1)而不是nx(x+1)/2。我不认为我看到了x在这里。@Garrick太棒了!修正了。
int x=0; //constant
for(int i=4*n; i>=1; i--) //runs n times, disregard the constant
x=x+2*i;
int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){ // runs n/3 times? how does it effect final answer?
z = z+5;
z++;
x = 2*x;
}
int y=0;
for(int j=1; j*j<=n; j++) //runs O(logn)? j <= (n)^1/2
y++; //constant
int b=0; //constant
for(int i=n; i>0; i--) //n times
for(int j=0; j<i; j++) // runs n+ n-1 +...+ 1. O(n^2)
b=b+5;
int y=1;
int j=0;
for(j=1; j<=2n; j=j+2) //runs n times
y=y+i;
int s=0;
for(i=1; i<=j; i++) // runs n times
s++;
int b=0;
for(int i=0; i<n; i++) //runs n times
for(int j=0; j<i*n; j++) //runs n^2 x n times?
b=b+5;
int x=0;
for(int i=1; i<=n; i=i*3){ //runs 1, 3, 9, 27...for values of i.
if(i%2 != 0) //will always be true for values above
for(int j=0; j<i; j++) // runs n times
x++;
}
int t=0;
for(int i=1; i<=n; i++) //runs n times
for(int j=0; j*j<4*n; j++) //runs O(logn)
for(int k=1; k*k<=9*n; k++) //runs O(logn)
t++;
int a = 0;
int k = n*n;
while(k > 1) //runs n^2
{
for (int j=0; j<n*n; j++) //runs n^2
{ a++; }
k = k/2;
}
int i=0, j=0, y=0, s=0;
for(j=0; j<n+1; j++) //runs n times
y=y+j; //y equals n(n+1)/2 ~ O(n^2)
for(i=1; i<=y; i++) // runs n^2 times
s++;
int a = 0;
int k = n*n*n;
while(k > 1) //runs O(logn) complexity
{
for (int j=0; j<k; j++) //runs n^3 times
{ a--; }
k = k/2;
}
int x=0; //constant
for(int i=4*n; i>=1; i--) //runs n times, disregard the constant
x=x+2*i;
int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){ // runs n/3 times? how does it effect final answer?
z = z+5;
z++;
x = 2*x;
}
int y=0;
for(int j=1; j*j<=n; j++) //runs O(logn)? j <= (n)^1/2
y++; //constant
int b=0; //constant
for(int i=n; i>0; i--) //n times
for(int j=0; j<i; j++) // runs n+ n-1 +...+ 1. O(n^2)
b=b+5;
int y=1;
int j=0;
for(j=1; j<=2n; j=j+2) //runs n times
y=y+i;
int s=0;
for(i=1; i<=j; i++) // runs n times
s++;
int b=0;
for(int i=0; i<n; i++) //runs n times
for(int j=0; j<i*n; j++) //runs n^2 x n times?
b=b+5;
int x=0;
for(int i=1; i<=n; i=i*3){ //runs 1, 3, 9, 27...for values of i.
if(i%2 != 0) //will always be true for values above
for(int j=0; j<i; j++) // runs n times
x++;
}
int t=0;
for(int i=1; i<=n; i++) //runs n times
for(int j=0; j*j<4*n; j++) //runs O(logn)
for(int k=1; k*k<=9*n; k++) //runs O(logn)
t++;
int a = 0;
int k = n*n;
while(k > 1) //runs n^2
{
for (int j=0; j<n*n; j++) //runs n^2
{ a++; }
k = k/2;
}
int i=0, j=0, y=0, s=0;
for(j=0; j<n+1; j++) //runs n times
y=y+j; //y equals n(n+1)/2 ~ O(n^2)
for(i=1; i<=y; i++) // runs n^2 times
s++;
int i=1, z=0;
while( z < n*(n+1)/2 )//arithmetic series, runs n times
{
z+=i; i++;
}
int a = 0;
int k = n*n*n;
while(k > 1) //runs O(logn) complexity
{
for (int j=0; j<k; j++) //runs n^3 times
{ a--; }
k = k/2;
}