Question

Describe in pseudocode an algorithm that takes as input a sequence of distinct numbers a0, a1, .....

Describe in pseudocode an algorithm that takes as input a sequence of distinct numbers a0, a1, ..., an and returns 1 if the sequence is ordered increasingly, -1 if the sequence is in decreasing order, and 0 otherwise.

What are the worse-case, best-case, and avergae-case of the algorithm? Analyze the running time in each of the three cases using asymptotic notation.

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

Function isOrderedIncreasingly(arr)

BEGIN

                // traverse the array

                for i = 1 to arr.length

                BEGIN

                                // if the i th element is greater than (i + 1) th element

                                If arr[i[ > arr[i + 1]

                                BEGIN

                                                return -1

                                END

                END

                return 0

END

The tie complexity in all the 3 cases, worse-case, best-case, and avergae-case is

O(n)

It is because in all the 3 cases, the entire array is traversed which takes O(n) time.

Add a comment
Know the answer?
Add Answer to:
Describe in pseudocode an algorithm that takes as input a sequence of distinct numbers a0, a1, .....
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