Question

Create a recurrence equation( T(n) ) and solve to get O(n) A(int n) if (n==1) time++;...

Create a recurrence equation( T(n) ) and solve to get O(n)

A(int n)

if (n==1)

time++;

else{

for i=1 to i->n

time++;

A(n-1)

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
A(int n)
if (n==1)
    time++;
else{
    for i=1 to i->n
        time++;
    A(n-1)
}

In each recursive call, a work(for loop) of n iterations is done. and a recursive call of size n-1 is made.
so, recurrence relation is T(n) = T(n-1) + O(n)

T(n)
= T(n-1) + n
= T(n-2) + n-1 + n
= T(n-3) + n-2 + n-1 + n
= T(1) + 2 + ... + n-2 + n-1 + n
= 1 + 2 + ... + n-2 + n-1 + n
= n(n+1)/2
so, T(n) = O(n^2)

so, time complexity is O(n^2)
Add a comment
Know the answer?
Add Answer to:
Create a recurrence equation( T(n) ) and solve to get O(n) A(int n) if (n==1) time++;...
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