How to sort a list into ascending order by value using recursion instead of a loop in python? For example, to sort [2,0,1] to [0,1,2].
def sort(a):
pos = 0
if pos == 0 or a[pos] >= a[pos - 1]:
pos += 1
return sort(a)
else:
a[pos], a[pos-1] = a[pos-1], a[pos]
pos -= 1
return sort(a)
Here is what I wrote and I know it does not work because the pos is always equal to 0 at first. How could I fix it?
I test the code below. enter image description here