确定大O符号 [英] Determining Big O Notation
问题描述
我需要帮助理解/干大O符号。我理解它的目的,我只是不知道如何确定给予了一块code中的复杂性。
确定大O符号为每个以下
一个。
N = 6;
COUT<&n种LT;< ENDL;
乙。
N = 16;
对于(I = 0; I&n种;我+ +)
COUT<< I<< ENDL;
℃。
I = 6;
N = 23;
而(ⅰn种){
的cout&其中;&所述I-6≤;&所述; ENDL;
我++;
}
Ð。
INT一个[] = {1,3,5,7,9,11,13,15,17,19};
N = 10;
对于(I = 0; I&n种;我+ +)
A [1] = A [1] * 2;
对于(i = 9; I> = 0;我 - )
的cout&其中;&其中;一[1] - ;&所述; ENDL;
如
总和= 0;
N = 6;
K = POW(2,N);
对于(i = 0; I< k;我++)
总和=总和+ K;
大O表示复杂性你的算法。
基本的东西:
- 在这种复杂的测量有关的条目大小
- 您选择一个单元操作(通常是做作或比较)
- 您算多少时间这种操作称为 使用复杂的时候,所以如果操作数是3 *
- 在一个常数项或常数因子通常被忽略ñ^ 3 + 12它简化为n ^ 3也标志着为O(n ^ 3)
)将只运行一次,没有循环,复杂性是微不足道这里 O(1)
B)拨打N次循环: O(N)
℃。)在这里我们选择来分析N(因为它通常是在一个算法递增变量)。呼叫数为n - 6所以这是 O(N)
ð。)让我们假设在这里,10(n)为您的阵列和九(i)本尺寸减一的大小。对于每一个值设置为n,我们有从0到去到n,则n-1 0 N *(N-1)的操作,技术上: O(N * 2)
这部分人大致为 O(N)
。这两个被称为线性时间,所不同的是它比戈不关心的直线的斜率。
如)循环从0到POW(2,N),也就是1到2的n次方,概括为 O(2 ^ n)的
I need help understanding/doing Big O Notation. I understand the purpose of it, I just don't know how to "determine the complexity given a piece of code".
Determine the Big O notation for each of the following
a.
n=6;
cout<<n<<endl;
b.
n=16;
for (i=0; i<n; i++)
cout<<i<<endl;
c.
i=6;
n=23;
while (i<n) {
cout<<i-6<<endl;
i++;
}
d.
int a[ ] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
n=10;
for (i=0; i<n; i++)
a[i]=a[i]*2;
for (i=9; i>=0; i--)
cout<<a[i]<<endl;
e.
sum=0;
n=6;
k=pow(2,n);
for (i=0;i<k;i++)
sum=sum+k;
Big O indicates the order of the complexity of your algorithm.
Basic things :
- This complexity is measured regarding to the entry size
- You choose a unit operation (usually affectation or comparison)
- You count how much time this operation is called
- A constant term or constant factor is usually ignored when using complexity so if the number of operation is 3*n^3 + 12 it's simplified to n^3 also marked O(n^3)
a.) Will just run once, no loop, complexity is trivial here O(1)
b.) Call n times in the loop: O(n)
c.) Here we choose to analyze n (because it's usually the incrementing variable in an algorithm). The number of calls is n - 6 so this is O(n)
.
d.) Let's suppose here that 10 (n) is the size of your array and nine (i) this size minus one. For each value to n, we have to go from 0 to n then n-1 to 0. n * (n-1) operations, technically: O(n * 2)
which some people approximate as O(n)
. Both are called Linear Time, what is different is the slope of the line which BigO doesn't care about.
e.) The loop goes from 0 to the pow(2, n), which is 1 to 2^n, summarized as O(2^n)
这篇关于确定大O符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!