Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Last active October 25, 2017 03:42
Show Gist options
  • Save sasaki-shigeo/8cd5e59a70673cd19c9aa9538ab7ef31 to your computer and use it in GitHub Desktop.
Save sasaki-shigeo/8cd5e59a70673cd19c9aa9538ab7ef31 to your computer and use it in GitHub Desktop.
data = [2, 9, 4, 8, 5, 6, 1]
def selection_sort(data, comp):
for k in range(1, len(data)):
for i in range(0, k):
if comp(data[i], data[k]) > 0:
temp = data[i]
data[i] = data[k]
data[k] = temp
def compare(x, y):
return x - y
selection_sort(data, compare) # ascendant order
print data
selection_sort(data, lambda x, y: y - x) # descendant order
print data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment