Skip to content

Instantly share code, notes, and snippets.

@multivac61
Last active October 3, 2015 15:16
Show Gist options
  • Save multivac61/60a7a625dba405ce8976 to your computer and use it in GitHub Desktop.
Save multivac61/60a7a625dba405ce8976 to your computer and use it in GitHub Desktop.
def selection_sort(m, sort_by=(lambda a, b: a < b)):
def find_next(m):
next_elem = m[0]
for i in m:
if sort_by(i, next_elem):
next_elem = i
return next_elem
if len(m) <= 1: # if list less than one element return
return m
initial_list = list(m) # deepcopy of passed in list
sorted_list = []
while initial_list:
next_min = find_next(initial_list) # find
sorted_list.append(next_min)
initial_list.remove(next_min)
return sorted_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment