Question

Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2 arbitrary integers. In the pseudo-code,

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

Since here we have to calculate T(n) as worst case time complexity . To calculate worst case time complexity

let's assume that for all i and j , both 'if' statements given in program is False because otherwise function will be returned to main function and we might not get worst case for T(n) .

We will Calculate time complexity line by line

1. n = A.size ( This line will take constant time lets O(1) because it is just an arithmetic operation.)

time complexity for any for loop is number of time inner statement is executed.

2. for i=1 to n

3. for j=1 to n

4. if A[n-j+1] != j then return (this line is a inner statement for both for loops)

5. if (A[i]!=n-i) or (A[1]+A[2] = 2*n-3) then return ( this line is also a statement)

in case of nested for loops time complexity is number of times inner statement is executed.

here for every value of i , for loop of line 3 executes from j=1 to n means 'n' number of times.

Therefore number of times inner loop executes is = (n+n+n......('n' times)) = n(1+1+1+.....n times) = n 2

Therefore for line 2,3,4,5 total time complexity will be O(n 2)

so total time complexity T(n) = O(n 2) + C = O(n 2)

Now question is why it is O(n 2) and not Ω(n 2) that is because our upper bound is n2 and we will never execute

both nested for loops greater than n2 times .

Therefore Your Answer is O(n 2).

Add a comment
Know the answer?
Add Answer to:
Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2...
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