Question

Show the Big O Complexity of the following functions and loop constructions: (Please show work and...

Show the Big O Complexity of the following functions and loop constructions: (Please show work and explain)

a. f(n) = 2n + (blog(n+1))

b. f(n) = n * (log(n-1))/2

c.

int sum = 0;
for (int i=0; i<n; i++)

sum++;
for (int j=n; j>0; j /= 2)

sum++;

d.

int sum = 0;
for (int i=n; i>0; i--)

for (int j=i; j<n; j *= 2)

sum++;

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

a.

f(n) = 2n + (blog(n+1))

It is also written as (2)n * (b) log(n+1)

Here 2 and b are constant values so it is negligible.

log(n+1) is equivalent to the log(n)

f(n) = n + logn

Here n is bigger than logn for any large value of n so logn is negligible.

f(n) complexity is O(n)

b.

f(n) = n * (log(n-1))/2

It is also written as n * (1/2) log(n-1)

Here 1/2 is constant value so it is negligible.

log(n-1) is equivalent to the log(n)

f(n) complexity is O(nlog n)

c.

int sum = 0; // It runs for 1 time so its complexity is O(1)
for (int i=0; i<n; i++) // It runs for n times so its complexity is O(n)

sum++; // It runs for every for loop so its complexity is O(n)


for (int j=n; j>0; j /= 2) // Here J is reduced to half every time so its complexity is O(log n)

sum++; // It runs for every for loop so its complexity is O(log n)

The complexity of whole program is O(n)

d.

int sum = 0; // It runs for 1 time so its complexity is O(1)
for (int i=n; i>0; i--)   // It runs for n times so its complexity is O(n)

for (int j=i; j<n; j *= 2) // Here j doubles every time so its complexity is O(logn) and also runs for every value of i so overall complexity is O(nlogn)

sum++; // Its complexity is O(nlogn)

sum++;

Overall complexity is O(nlogn)

Add a comment
Know the answer?
Add Answer to:
Show the Big O Complexity of the following functions and loop constructions: (Please show work and...
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