Identify the unit count for each portion of the code given below to identify the
ID: 3665708 • Letter: I
Question
Identify the unit count for each portion of the code given below to identify the overall Big Oh speed. Use the example below as a template for your answer, or some classes may use summations. The table answers below are NOT correct!! But the overall setup is. Partial credit will be given if some portion of the work shown is correct.
Code
Cost
# of Times
Total
int sum1(int N)
{
int s = 0;
for(int i = 1; i <= N; i++)
{
s = s + i;
}
return s;
}
-
-
1
1
1
-
-
1
1
1
1 (int i) + 1 (<=) + 1 (++)
1 + 2(n + 1)
2n + 3
-
-
2
n
2n
-
-
-
-
1
1
1
-
-
Final
4n + 6
O(n)
FOR THE CODE BELOW
sum = 0;
for( i = 0; i < n; i += 2 )
for( j = 0; j < n; j++ )
++sum;
Code
Cost
# of Times
Total
int sum1(int N)
{
int s = 0;
for(int i = 1; i <= N; i++)
{
s = s + i;
}
return s;
}
-
-
1
1
1
-
-
1
1
1
1 (int i) + 1 (<=) + 1 (++)
1 + 2(n + 1)
2n + 3
-
-
2
n
2n
-
-
-
-
1
1
1
-
-
Final
4n + 6
O(n)
Explanation / Answer
portion of the work shown is correct.
Code
Cost
# of Times
Total
sum = 0;
for( i = 0; i < n; i += 2 )
for( j = 0; j < n; j++ )
++sum;
-
-
-
-
-
-
1
1
1 (int i) + 1 (<=) + 1 (+=2)
1+2(n/2 + 1)
n+3
1 (int i) + 1 (<=) + 1 (++)
(n/2)*(1+2(n + 1)
n2+3n/2
1
n2/2
-
-
-
-
-
-
-
Final
3n2/2+5n/2 + 4
O(n2)
Code
Cost
# of Times
Total
sum = 0;
for( i = 0; i < n; i += 2 )
for( j = 0; j < n; j++ )
++sum;
-
-
-
- --
-
-
- -1
1
11 (int i) + 1 (<=) + 1 (+=2)
1+2(n/2 + 1)
n+3
1 (int i) + 1 (<=) + 1 (++)
(n/2)*(1+2(n + 1)
n2+3n/2
1
n2/2n2/2
-
-
-
-
-
-
-
Final
3n2/2+5n/2 + 4
O(n2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.