Question

Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are intege

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

(1):

Time complexity of IterativeFunctioin is O(n/2) which is equivalent to O(n) because every time we decrease the value of "a" by 2, so it takes (n/2) time to complete the code.

Space complexity of IterativeFunctioin is O(1) because we have used constant space like only variable a and b .

(2):

In the given code,

for i=0, j loop iterates from 0 to n-1, which is for n times and k loop iterates for n times. So for i=0, it takes n*n iterations.

for i=1, j loop iterates from 1 to n-1, which is for n-1 times and k loop iterates for n times. So for i=1, it takes n*(n-1)iterations.

.

.

for i=n-1, j loop iterates from n-1 to n-1, which is for 1 time and k loop iterates for n times. So for i=0, it takes niterations.

So the answer is n + n*2 + n*3 + .... + n*n = n[1+2+...+n] = n[n(n+1)/2] = O(n3)

So T(n) = O(n3)

Time complexity of given code is \theta (n3)

(3):

Algorithm is as follows:

  • Find the index of minimum value in array. (It takes O(n) time to complete the task)
  • Next swap values present at index 0 and minimum value index which takes O(1) time

So the time complexity of this efficient algorith is O(n)+O(1) = O(n)

Time complexity : O(n)

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...
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