Question

def sorted(numbers): num = numbers[0] for i in numbers: if i < num: return False num...

def sorted(numbers):
num = numbers[0]
for i in numbers:
if i < num:
return False
num = i
return True

State a best-case and a worst-case scenario for your algorithm. Explain the Big-O complexity of each scenario. Use comparison as the basic unit of computation. Take into account the complexity of any list operation you use

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Best case:
------------
if the first two numbers are not in sorted order
which means if i < num for first number in numbers list, then the function directly return False.
so, in this case only one statement was executed.
so, in best case time complexity is O(1)
Answer: O(1)

Worst case:
------------
If the list is sorted, then all of the i < num is not True.
so, it iterates n times if the numbers list is sorted.
so, in worst case time complexity is O(n)
Answer: O(n)
Add a comment
Know the answer?
Add Answer to:
def sorted(numbers): num = numbers[0] for i in numbers: if i < num: return False num...
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