第二章 Big O notation 试题以及讲解 (包会)
2015-03-07 14:24
267 查看
这里列出了一些难做的题或是易错的题,简单的我就没有过多的解释,但是如果大家有任何问题都可以私信我或是评论一下,我会尽量即时的解答问题或是疑问的。
int x=0;
for(int i=4*n; i>=1; i--)
x=x+2*i;
O(n)
The loop runs O(n) times and does O(1) work per iteration.
int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){
z = z+5;
z++;
x = 2*x;
}
O(log n)
Think about the values of i as the loop progresses. It will take on the series of values 1, 3, 9, 27, 81, 243, ..., 3k. Since i is tripling on each iteration, it takes on successive powers of three.
The loop clearly only does O(1) work per iteration, so the main
question here is how many total iterations there will be. The loop
will stop when i > n. If we let k be some arbitrary iteration of the
loop, the value of i on iteration k will be 3k. The loop stops when
3k > n, which happens when k > log3 n. Therefore, the number of
iterations is only O(log n)
int y=0;
for(int j=1; j*j<=n; j++)
j <= (n)^1/2 y++;
O(√n)
Notice that j is still growing linearly, but the loop runs as long as
j2 ≤ n. This means that as soon as j exceeds √ n, the loop will
stop. Therefore, there will only be O(√n) iterations of the loop,
and since each one does O(1) work, the total work done is O(√n)
int b=0; //constant
for(int i=n; i>0; i--)
for(int j=0; j<i; j++)
b=b+5;
O(n^2)
The most accurate answer would be O(n2)
int y=1;
int j=0;
for(j=1; j<=2n; j=j+2)
y=y+i;
int s=0;
for(i=1; i<=j; i++)
s++;
O(n)
int b=0;
for(int i=0; i<n; i++)
for(int j=0; j<i*n; j++)
b=b+5;
The inner loop will run 0 + n + 2n + 3n + 4n + ... + n(n-1) = n(0 + 1
+ 2 + ... + n - 1) times, so the total work done is O(n3). You
shouldn't multiply by the number of times the outer loop runs because
you're already summing up across all iterations. The most accurate
runtime would be O(n3)
int t=0;
for(int i=1; i<=n; i++)
for(int j=0; j*j<4*n; j++)
for(int k=1; k*k<=9*n; k++)
t++;
Look at the second loop. This actually runs O(√n) times using the
same logic as one of the earlier parts. That third inner loop also
runs O(√n) times, and so the total work done will be O(n2)
int a = 0;
int k = n*n;
while(k > 1)
{
for (int j=0; j<n*n; j++)
{ a++; }
k = k/2;
}
The outer loop starts with k initialized to n2, but notice that k is
halved on each iteration. This means that the number of iterations of
the outer loop will be log (n2) = 2 log n = O(log n), so the outer
loop runs only O(log n) times. That inner loop does do O(n2) work, so
the total runtime is O(n^2 log n)
int i=0, j=0, y=0, s=0;
for(j=0; j<n+1; j++)
y=y+j;
for(i=1; i<=y; i++)
s++;
O(n^3)
int x=0;
for(int i=4*n; i>=1; i--)
x=x+2*i;
O(n)
The loop runs O(n) times and does O(1) work per iteration.
int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){
z = z+5;
z++;
x = 2*x;
}
O(log n)
Think about the values of i as the loop progresses. It will take on the series of values 1, 3, 9, 27, 81, 243, ..., 3k. Since i is tripling on each iteration, it takes on successive powers of three.
The loop clearly only does O(1) work per iteration, so the main
question here is how many total iterations there will be. The loop
will stop when i > n. If we let k be some arbitrary iteration of the
loop, the value of i on iteration k will be 3k. The loop stops when
3k > n, which happens when k > log3 n. Therefore, the number of
iterations is only O(log n)
int y=0;
for(int j=1; j*j<=n; j++)
j <= (n)^1/2 y++;
O(√n)
Notice that j is still growing linearly, but the loop runs as long as
j2 ≤ n. This means that as soon as j exceeds √ n, the loop will
stop. Therefore, there will only be O(√n) iterations of the loop,
and since each one does O(1) work, the total work done is O(√n)
int b=0; //constant
for(int i=n; i>0; i--)
for(int j=0; j<i; j++)
b=b+5;
O(n^2)
The most accurate answer would be O(n2)
int y=1;
int j=0;
for(j=1; j<=2n; j=j+2)
y=y+i;
int s=0;
for(i=1; i<=j; i++)
s++;
O(n)
int b=0;
for(int i=0; i<n; i++)
for(int j=0; j<i*n; j++)
b=b+5;
The inner loop will run 0 + n + 2n + 3n + 4n + ... + n(n-1) = n(0 + 1
+ 2 + ... + n - 1) times, so the total work done is O(n3). You
shouldn't multiply by the number of times the outer loop runs because
you're already summing up across all iterations. The most accurate
runtime would be O(n3)
int t=0;
for(int i=1; i<=n; i++)
for(int j=0; j*j<4*n; j++)
for(int k=1; k*k<=9*n; k++)
t++;
Look at the second loop. This actually runs O(√n) times using the
same logic as one of the earlier parts. That third inner loop also
runs O(√n) times, and so the total work done will be O(n2)
int a = 0;
int k = n*n;
while(k > 1)
{
for (int j=0; j<n*n; j++)
{ a++; }
k = k/2;
}
The outer loop starts with k initialized to n2, but notice that k is
halved on each iteration. This means that the number of iterations of
the outer loop will be log (n2) = 2 log n = O(log n), so the outer
loop runs only O(log n) times. That inner loop does do O(n2) work, so
the total runtime is O(n^2 log n)
int i=0, j=0, y=0, s=0;
for(j=0; j<n+1; j++)
y=y+j;
for(i=1; i<=y; i++)
s++;
O(n^3)
相关文章推荐
- Big O notation大零符合 入门讲解
- 第二章 Big O notation
- tesseract-ocr识别英文和中文图片文字以及扫描图片实例讲解
- forward请求转发,param参数传递以及request.getParameter图文讲解
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典)
- Mysql复制实现,读写分离讲解以及安装失败
- log4cxx配置文件讲解,以及测试程序
- 详细讲解css单位px,em和rem的含义以及它们之间的区别
- iOS 开发中遇到的各种证书的详细说明以及申请讲解
- java泛型操作复习,以及讲解在android中使用的场景
- 【无私分享:ASP.NET CORE 项目实战(第二章)】添加EF上下文对象,添加接口、实现类以及无处不在的依赖注入(DI)
- 通过短信窃听器来讲解内容提供者,内容观察者,以及无界面后台运行服务,开机启动和杀死服务后重新启动
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- Determining Big O Notation
- C#整理面试试题分类以及sql数据库(含答案)
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典)
- hdu3555Bomb以及对数位dp的讲解
- SEO技巧:关于SEO的详细讲解以及SEO的日常工作重点
- Linux C编程第二章的10个问题以及解决方案