Skip to content

Commit 702450d

Browse files
authored
Update recipe-576917.py
Added the recursive version from the comments at https://code.activestate.com/recipes/576917-functional-selection-sort/#c1
1 parent 0630f5a commit 702450d

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

recipes/Python/576917_Functional_selection_sort/recipe-576917.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
test_list = [ 5, 1, 7, 0, -3, -10, 10, -6, 1, 0, 2, 4, -2, 6, 5, 8, 2]
1616
print test_list
1717
print selection_sort(test_list)
18+
19+
# Recursive version
20+
def select_sort_r(L):
21+
if not L: return [] # terminal case
22+
idx, v = min(enumerate(L), key=lambda e: e[1]) # select the minimum
23+
return [v] + select_sort_r(L[:idx] + L[idx+1:]) # recursively call on the remainder

0 commit comments

Comments
 (0)