provide your performance estimate. Comment on other student responses providing
ID: 666834 • Letter: P
Question
provide your performance estimate. Comment on other student responses providing additional insight and details as needed.
What is the big-O performance estimate of the following function?
int f (n) {
int sum = 0;
for (i = 0; i < n; i++)
sum += i;
return sum;
} // end f
.
What is the big-O performance estimate of the following function?
int f (n) {
int sum = 0;
for (i = 0; i < n; i = i + 10)
sum += i;
return sum;
} // end f
.
What is the big-O performance estimate of the following function?
int f (n) {
int sum = 0;
for (i = 0; i < n*n; i++)
sum += i;
return sum;
} // end f
Explanation / Answer
1) O(n)
value of i in each iteration: suppose n=5
0,1,2,3,4
for loop executes 5 times i.e n times
for loop iterate n times,hence order of growth will be O(n) times.
2) O(log n)
value of i at each iteration: suppose n=40
0,10,20,30
in this case for loop will execute (log n) times.
3) O(n^2)
value of i in each iteration: suppose n=2
0,1,2,3
here n is 2 but loop iterates 4 times i.e (n^2) times
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.