Identify the unit count for each portion of the code given below to identify the
ID: 3665647 • 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)
MAKE THE TABLE FOR THIS EXAMPLE
#4
sum = 0;
for ( i = 0; i < n; i++ )
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
first of all I explained your questions in brief...
The running time for the operation sum++ is a constant. The outer loop runs n times, The nested loop also runs n times, hence the complexity would be O( n 2 ) .
n2 + n +1
O(n2)
final answers is O(n2).
Code Cost Of times Total Sum=0 - - - for(i=0;i<n;i++) o(i)+o(<)+o(++) o+(n) n for(j=0;j<n;j++) o(j)+o(<)+o(++) o+n(n) o(n2) ++sum 1 1 1 Finaln2 + n +1
O(n2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.