Efficiency of Algorithms : Determine the order of magnitude for each piece of co
ID: 3144556 • Letter: E
Question
Efficiency of Algorithms :
Determine the order of magnitude for each piece of code. 1 additional bonus point for giving the precise Big O. When giving precise Big O you can exclude the looping structures from the statement. You should take them into consideration only to determine the number of times the inner part runs.
Sample#1:
int i = 0;
int j = 1;
Sample #2:
for(int i = 0; i < n; i++){
sum += i;
}
int j = 0;
while(j < n){
sum--;
j++; }
Sample #3:
for(int i = 0; i < n; i++){
for(int j = 0;
j < n; j++){
sum += i;
}
Sample #4:
for (int i = 0; i < n; i++) { sum += i;
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) { sum--;
}
}
Explanation / Answer
For Sample 1
Order is 0 as no operation is to be performed. Thus, O(0)
For sample 2
As N grows, complexity will grow, there are four operations. Thus order is 4. O(NxNxNxN)
For sample 3
Here also, the order is O(NxNxNxN)
For sample 4
Order is given by O(NxNxNxNxN) as there are 5 operation to be performed for each N
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.