Question

Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1

Using the pseudocode answer these questions

4. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a worst-case input of size n. Show your work. 5. C

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

in the given code, we can see that i iterates from 0 to n-1 which is n-2 times

in all these n-2 times, the given condition might not be satisfied and finally, we return -1

so in the worst case, we make n-2 comparisons

In the best case, the comparison is done only 1 time

that is when A[0]A[1]> A[2]

we will return 0 in the best case as the value of i =0

Add a comment
Know the answer?
Add Answer to:
Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do...
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