Question

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

while (j <= n) {

sum++;

j *= 2;

}

Big-O Category

Justification (why did you pick the way you did?)

3.2

int sum = 0;

for (int j = 1; j < n; j++) {

sum++;

if (j % 2 == 0) {

    sum++;

}

}

Big-O Category

Justification (why did you pick the way you did?)

3.3

int sum = 0;

for (int i = 1; i <= n * 2; i++) {

for (int j = 1; j <= n; j++) {

    sum++;

}

}

Big-O Category

Justification (why did you pick the way you did?)

3.4

for (int j = 1; j < 100; j++) {

sum++;

sum++;

}

Big-O Category

Justification (why did you pick the way you did?)

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

3.1) The complexity of the algorithm is O(log(n))

All the single step execution path are considered as execution time of O(1), so

int sum = 0;------->O(1)

int j = 1;--------------> O(1)

Then while loop is executed, the value of j is the series, 1, 2,2^2,2^3,.....,2^k

If we put k equals to Log2n, we get 2Log2n which is n.

ie the time complexity of a code isO( log(n)) when loop variable is multiplied or divided by a constant value

------------------------------------------------------------------------------------------------------------------------------

3.2) Here the for loop executes n times and so the code inside the for loop.

So the final time complexity will be O(n) + O(1) = O(n)

Note here O(1) is the time complexity of code outside for loop

--------------------------------------------------------------------------------------------------------------------------------

3.3)Time complexity of code outside nested for loops= O(1)

Here we have nested for loop, so to find the time complexity , find the number of time inner for loop is executed.

Inner for loop is executed 2*n times

and complexity of inner for loop =O(n) as it executes codes inside it n times.

So total time complexity= O(1)+O(2n*n)=O(2n^2)= O(n^2), we can eliminate constant 2

----------------------------------------------------------------------------------------------------------------------------------

3.4)

In this case the loop variable i executes 100 times and so the code inside the for loop

So the time complexity of algo =O(100)= O(1), because complexity of any algorithm executing a constant number of times i can be considered as O(1)

Add a comment
Know the answer?
Add Answer to:
. Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition. Estimate the big-O complexity for...
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