Skip to content

Instantly share code, notes, and snippets.

@czheo
Created February 2, 2017 06:10
Show Gist options
  • Save czheo/bc0d61fb9efd82effe3163fc8364113f to your computer and use it in GitHub Desktop.
Save czheo/bc0d61fb9efd82effe3163fc8364113f to your computer and use it in GitHub Desktop.
def selection_sort(lst):
i = 0
n = len(lst)
while i < n:
min_pos = i
for j in range(i, n):
if lst[j] < lst[min_pos]:
min_pos = j
target = lst[min_pos]
j = min_pos
while j > i:
lst[j] = lst[j-1]
j -= 1
lst[i] = target
i += 1
lst = [3,2,2,4,5,6,7,9,9,6,4,2,1]
selection_sort(lst)
print(lst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment