110k views
1 vote
Give a recursive implementation for the function: def is_sorted(lst, low, high) This function is given a list of numbers, lst, as well as two indices: low and high (low ≤ high), which indicate the range of the indices for the elements that should to be considered. When called, the function should determine if the elements that are placed at the low, low+1, …, high positions, are in an (ascending) sorted order. That is, it should return True if-and-only-if lst[low] ≤ lst[low+1] ≤ … ≤ lst[high] For example, if lst = [1, 3, 6, 8, 12, 15, 31], the call is_sorted(lst, 0, 6), will return True. Implementation requirements: Your function should run in worst case linear time. That is, if n is the size of the range low, low+1, …, high, calling is_sorted(lst, low, high) will run in �(�). Note: Write your implementation.

User Echoblaze
by
5.0k points

1 Answer

3 votes

Answer:

# recursive method to find if list is in ascending order

def is_sorted(list, low, high):

if low >= high: # if reached end of list

return True

if list[low] > list[low+1]: # if item at low is greater than low+1

return False # return false

return True and is_sorted(list, low+1, high) # or return True and recursion call to low+1

Step-by-step explanation:

User Jairaj
by
4.2k points