## kvard

Hello,

I've stumbled upon question I can't solve on my own. What I need is to write a program in python that would perform selection sort, but here's trick - it has to be recursive.

So, for example I have
l = [3,2,1]
after using sort(l), l = [1,2,3]

I've this far:

def sort(l):
if len(l) <= 1:
return l
else:
l.append(#the largest element#)
return selsort(l[1:])

So, basically I don't know how to extract maximum element using recursion. If anyone knows how to do - pls help :)

## masterofpuppets 19

hi,
here's a hint:

you can find the largest element using a for loop like this:

maxV = l[ 0 ]   # l is the array you call the function with
for el in l:
if el > maxV:
maxV = el

print maxV
# -> should output the largest number in the list

the tricky part in the recursion sort algorithm, at least for me, was that once you place the element in first place, you have to start from the second element the next time, so you'll need a counter that keeps track of which element you're currently checking. Here's what I mean:

selectionSort( [ 4, 2, 6 ] ) would do something like this:
1) find the largest/smallest element
2) replace it with the element in first/last place
3) call selectionSort( with the array starting from 1 not from 0, because 0 is already occupied by the largest/smallest element )

hope this doesn't make it too confusing :)

if you need some more help, just post a message with your code here and pls use code tags next time :)

Hi,