Write a recursive version of insertionsort. The function shouldn’t have any loops? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)):

29th Apr 2018, 2:56 PM
Eric
1 Answer
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.
11th May 2018, 4:34 AM
Henrik Ohlsson
Henrik Ohlsson - avatar