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
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
ADVERTISEMENT