Question

Specify the running time of each of the following algorithms. You must fully explain your answer for each of the question!

Algorithm Ex1(ai,.., an), a): for i ← 1 to n do If ai 〉 a raj return x Algorithm Ex2((ai ,..., an), (b,..., bm)): for i ← 1 to min(n, m) do ifai 〈bi return x

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

a) O(n)
Explanation : We can see that for loop will run from 1 to n so it will run n+1 times. One time extra because when i will be n+1 so condition will fail. Statements inside for loop will run for n time. So time complexity of algorithm is O(n)


b)  O(min(n,m))
Explanation : We can see that for loop will run from 1 to min ( n, m) so it will run min(n,m)+1 times. One time extra because when i will be n+1 or m+1 so condition will fail. Statements inside for loop will run for min(n,m) time. So time complexity of algorithm is O(min(n,m))

c)  O(mnpq)
Explanation : 4 nested loop, One inside the other . Each loop will run for its range time and a,ong iwth its range because of outer loop. Hence the time complexity is O(mnpq)

d)  O(n4)
Explanation : 4 nested loop, One inside the other . Outer loop will run for n time, the first inner loop will run for n time itself and n times because of outer loop so its n2 . Similarly 2nd inner loop will run for n3 time, and the inner most loop will run for  n4 time. Hence the time complexity is O(n4 )


(e)O(nm)
Explanation While loop will run for n time, and inner loop loop will run for m time itself and n time because of outer loop so its nm time. hence the time complexity is O(nm)


Thanks, let me know if there is any concern. PLEASE RATE if you think its helpful

Add a comment
Know the answer?
Add Answer to:
Specify the running time of each of the following algorithms. You must fully explain your answer...
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