What is the Big-O running time for this code? Explainyour answer. for (int i=0;
ID: 3616375 • Letter: W
Question
What is the Big-O running time for this code? Explainyour answer.for (int i=0; i<num; i++) { rate = NORMAL_RATE; for (int j=0; j<num2; j++) if ( j > 8 ) rate =OVERTIME_RATE; System.out.println("Day " + i + " hour " + j + " rate " + rate); }
10. What is the Big-O running time for this code? Explain your answer.
if ( num > 30 ) for (int j=0; j<num;j++) System.out.println( j); else System.out.println("More than30 days");
What is the Big-O running time for this code? Explainyour answer.
for (int i=0; i<num; i++) { rate = NORMAL_RATE; for (int j=0; j<num2; j++) if ( j > 8 ) rate =OVERTIME_RATE; System.out.println("Day " + i + " hour " + j + " rate " + rate); }
10. What is the Big-O running time for this code? Explain your answer.
if ( num > 30 ) for (int j=0; j<num;j++) System.out.println( j); else System.out.println("More than30 days");
Explanation / Answer
a) The outer for loop runs "num" times and the inner for loop runs num2 Therefore in total..the number of iterations equals (num *num2) Assuming Maximum(num, num2) = n Total number of iterations are n2 Therefore the complexity of this piece of code isO(n2) b) Supposing the it enters IF loop....it will enter the FORloop For loop will run for "num" times Assuming num =n it will run for n times Therefore the complexity of this code snippet is O(n)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.