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.

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;

Page 5 of 6

while (i < n) {

i++;

j = i;

while (i < n) {

printf("hello %d\n", i);

i++;

break;

}

i = j;

}

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

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");

}

}
================================================================
Answer: O(n2)
Explanation: As
we can see there are two for loop one nested on another. Lets say if n = 3, the program will run for 3*3 => 9 times. So the idea if the upper for loop will run for n time and then inner runs each n times for every iteration of upper for loop
n + n + n + n + n .. ..n times => n*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);

}

}
================================================================
Answer: O(n2)
Explanation: As
we can see there are two for loop one nested on another. Lets say if n = 3, the program will run for 3*3 => 9 times. So the idea if the upper for loop will run for n time and then inner runs each n times for every iteration of upper for loop
n + n + n + n + n .. ..n times => n*n
If ith n =
1000000 and for jth we have 1000 , Then TOTAL TIMES = 1000000 * 1000 = 109
=================================================================

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;

}

================================================================
Answer: O(n2)
Explanation: As
we can see there are two while loop one nested on another. Lets say if n = 3, the program will run for 3*3 => 9 times. So the idea if the upper while loop will run for n time and then inner runs each n times for every iteration of upper for loop
n + n + n + n + n .. ..n times => n*n
=================================================================

Part d)

int i = 0;

int n = 10;

int j;

Page 5 of 6

while (i < n) {

i++;

j = i;

while (i < n) {

printf("hello %d\n", i);

i++;

break;

}

i = j;

}
================================================================
Answer: O(n)
Explanation: As
we can see there are two for loop one nested on another.So the idea if the upper while loop will run for n time and then inner runs once once as there is break statement
1 + 1 + 1 + 1 + 1 + ...n times = n
=================================================================

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 (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++)...

  • 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]...

  • . 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 =...

  • (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...

  • 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)

  • 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...

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • 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...

  • **C++ Question** For all of the following, determine the total operation count and then the Big-O...

    **C++ Question** For all of the following, determine the total operation count and then the Big-O of the given code segments: a. for (int j = 0; j < n; j++) for (int k = 0; k < j; k++) sum++; b. for (int i = 0; i < q*q; i++) for (int j = 0; j < i; j++) sum++; For all of the following, just determine the Big-O of the given code segments: c. for (int i =...

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