Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

def recursive_selSort (L): \" \"Selection sort, recursive argmax version. This s

ID: 3733701 • Letter: D

Question

def recursive_selSort (L): " "Selection sort, recursive argmax version. This solution is suboptimal for efficiency, because of all the cloned lists you have to create (which require expensive copying). So in practice you would not use recursion for selection sort. But it is a good intellectual exercise to better understand recursion. Algorithm: - base case: ifLis length 0 or 1, solve directly [do a small amount of work] - compute maximum of entire list - clone the list (before you change it, to avoid changing it outside) - in this cloned list, swap max to the last position - store this max elt (so that you can append it after the recursive call) - recursively sort a prefix of the list, by slicing out the last element (note that this is a smaller list since it doesn't include last element) mop up - append the original maximum element to the recursively sorted list - return this list, which is the entire sorted list Params: L (list, int or float) Returns: (list, same type as L) new sorted list pass # ENTER YOUR CODE HERE A sortedrecursive_selSort (A) print ("Sorted 1ist:"+ str(A-sorted)) from random import* def genInts (n, lo, hi) " " "Generate a random list of integers Params: n (int) #integers lo, hi (int): choose ints in the interval [lo, hi], lo

Explanation / Answer

Here I am just implementing the required methods, recursiveSelectionSort

here n is the length of the array.

Code:

def minIndexFromArray( a , m , n ):

if m == n:

return m

k = minIndexFromArray(a, m + 1, n)

return (i if a[m] < a[k] else k)

def recursel_Sort(n, index = 0):

if index == n:

return -1

k = minIndexFromArray(L, index, n-1)

if k != index:

L[k], L[index] = L[index], L[k]

recurSelectionSort(n, index + 1)