Question

For the following parts, try to get the best Big-O estimate that you can and briefly...

For the following parts, try to get the best Big-O estimate that you can and briefly justify your
answers (3-4 sentences per part). You should also consider running times for all the operations
contained within the loop body (but ignore the running times for initializer, entry condition and
increment).
Part a
int i, j;
int n = 100 ;
for (i = 1 ; i <= n; i++) {
for ( j = 3 *i ; j <= n; j++) {
printf( "programming is
fun\n" ) ;
}
}

Part b
int i, j;
int n = 1000000 ;
for (i = 1 ; i <= n; i++) {
for (j = 1 ; j <= 10000 ; j++)
{
printf ( "%d %d\n" , i, j);
}
}

Part c
int i = 0 ;
int n = 10 ;
int j;
while (i < n) {
i++ ;
j = i ;
while (i < n) {
printf( "hello %d\n" ,
i) ;
i++ ;
}
i = j;
}

Part d
int i = 0 ;
int n = 10 ;
int j;
while (i < n) {
i++ ;
j = i ;
while (i < n) {
printf( "hello %d\n" ,
i) ;
i++ ;
break;
}
i = j;
}
Page

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As the question contains more than 1 question and all the questions are independent so I would be solving only the 1st question(following the guidelines of Chegg).

a) As the loops are nested(one loop is inside the other) ,so the overall complexity involved would be the multiplication of the individual complexities of the loops.

As the outer loop runs from 1 to n with increment of 1 so,the complexity of the outer loop would be O(n).

As the inner loop runs from 1 to n with increment of 1 and just the initialization is from i*3 which doesn't make any difference when the n value would be large ,so its complexity would be O(n).

As discussed above ,the overall complexity would be the multiplication of the individual complexities,so overall complexity is O(n)*O(n) that given complexity as O(n2).

Add a comment
Know the answer?
Add Answer to:
For the following parts, try to get the best Big-O estimate that you can and briefly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the following parts, try to get the best Big-O estimate that you can and briefly...

    For the following parts, try to get the best Big-O estimate that you can and briefly justify your answers. Part a) int i, j; int n = 100; for (i = 1; i <= n; i++) { for (j = 3*i; j <= n; j++) { printf("programming is fun\n"); } } Part b) int i, j; int n = 1000000; for (i = 1; i <= n; i++) { for (j = 1; j <= 10000; j++) { printf("%d %d\n",...

  • Page 3 of 5 Question 3 (20 marks, each part is 5 marks) For the following...

    Page 3 of 5 Question 3 (20 marks, each part is 5 marks) For the following snippets, how many times is the printf statement executed? Briefly explain (up to 3 sentences). Part a int i, j; int n = 100; for (i = 1; i <= n; i++) { for (j = 3*i; j <= n; j++) { printf("programming is fun\n"); } } Part b int i, j; int n = 1000000; for (i = 1; i <= n; i++)...

  • For each of the below code snippets, identify the bounding function (the big O) of the...

    For each of the below code snippets, identify the bounding function (the big O) of the number of times the indicated line is run (justify your answer briefly): int i = 1: while (i < n) { printf ("Insert difficult work here!") i = i + i: } for(i = 0: i < n: i++) { for (j = 0: j < n: j++) { for (k = 0: k < n: k++) { if(i==j && j==k) arr[i] [j] [k]...

  • For the following program fragment give a Big-O analysis of the running time. Briefly explain your...

    For the following program fragment give a Big-O analysis of the running time. Briefly explain your answer: int t = 0; for(int i=1; i <= n; i++) for(int j=1; j <= i*i; j++)   if(j % i == 0)    t++;    What I have so far, O(1) + O(n) + O(n2) + O(1) + O(1) Drop Low order terms: O(n) + O(n2) And I believe the final answer to be O(n3), but not sure if just drop the O(n) or...

  • . Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition. Estimate the big-O complexity for...

    . Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition. Estimate the big-O complexity for each of these algorithms, and justify your answer. To confirm your calculations, answers are provided at the end of the rubric. Your justification can be mathematical or written, formal or informal. Rubric: Correct Big-O classification of four problems Justification of four problems Big-O categories: 3.1. O(log n). 3.2. O(n). 3.3. O(n2). 3.4. O(1) Problem Code fragment 3.1 int sum = 0; int j =...

  • 1. Determine the appropriate big-o expression for each of the following functions, and put your answer...

    1. Determine the appropriate big-o expression for each of the following functions, and put your answer in the table we have provided in section 2-1 of ps5_parti. We've included the answer for the first function. (Note: We're using the “ symbol to represent exponentiation.) a (n) = 5n + 1 b. b(n) = 5 - 10n - n^2 o c(n) = 4n + 2log (n) d. e. d(n) = 6nlog (n) + n^2 e(n) = 2n^2 + 3n^3 - 7n...

  • Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question...

    Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question (Big O): Please explain me in details the order of growth (as a function of N) of the running times of each of the following code fragments: a)         int sum = 0; for (int n = N; n > 0; n /= 2)    for(int i = 0; i < n; i++)      sum++; b)         int sum = 0; for (int i =...

  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...

    Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise { public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return...

  • (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity...

    (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity i) for (int i = 0; ǐ < n/2; i++) for (int j -0: ni j++) count++ i) for (int í = 0; i < n; i++) for (int ni j0 - for (int k j k ni kt+) count++ İİİ) for (int í ー 0; i < n; i++) for(int j = n; j > 0; j--) for (int k = 0; k...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT