Skip to content

Instantly share code, notes, and snippets.

@emre
Created August 4, 2013 22:12
Show Gist options
  • Select an option

  • Save emre/6152161 to your computer and use it in GitHub Desktop.

Select an option

Save emre/6152161 to your computer and use it in GitHub Desktop.
selection sort
def selection_sort(unsorted_list):
"""
>>> unsorted_list = [2, 1, 9, 8, 89]
>>> selection_sort(unsorted_list)
>>> unsorted_list
[1, 2, 8, 9, 89]
"""
for i in range(len(unsorted_list)):
smallest_index = get_smallest_index(unsorted_list, i)
unsorted_list[i], unsorted_list[smallest_index] = unsorted_list[smallest_index], unsorted_list[i]
def get_smallest_index(unsorted_list, index):
smallest_index = index
for i in range(index, len(unsorted_list)):
if unsorted_list[i] < unsorted_list[smallest_index]:
smallest_index = i
return smallest_index
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment