+ 2
Write a recursive version of insertionsort. The function shouldn’t have any loops?
Def instertionSort(lst): K = 1 While k <len(lst): x= lst.pop(k) insertInOrder(lst, k, x) k+=1 The above function insertionSort sorts (arranges the elements of) lst in ascending order, using the Insertion Sort algorithm. It relies on the function insertInOrder, which assumes that the first k elements of lst are already sorted and inserts x in the appropriate place among them: def insertInOrder (lst, k, x): while k>=1 and lst[k-1]>x: K -= 1 lst.insert(k,x) —————————- def insertionSort(lst, n=len(lst)):
1 Respuesta
0
It looks like you want to solve it recursively and not by iteration. Look up "recursively defined functions" at Wikipedia or so, they have decent pseudo code to get you started.
Oh, and use if() instead of while(), divide into three "cases", that's how I usually solved these types of problems.