Skip to content

Instantly share code, notes, and snippets.

@ducin
Created February 16, 2014 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/9039870 to your computer and use it in GitHub Desktop.
Save ducin/9039870 to your computer and use it in GitHub Desktop.
selection sort implementation
def selection_sort(elements):
size = len(elements)
# loop over all elements excluding last one
for i in range(size - 1):
# find the smallest value in current range
min_index, min_value = i, elements[i]
for j in range(i + 1, size):
if elements[j] < min_value:
min_index, min_value = j, elements[j]
# swap i-th element with the smallest one
tmp = elements[i]
elements[i] = elements[min_index]
elements[min_index] = tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment