Skip to content

Instantly share code, notes, and snippets.

@emre
Created August 5, 2013 08:47
Show Gist options
  • Select an option

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

Select an option

Save emre/6154388 to your computer and use it in GitHub Desktop.
insertion sort
def insertion_sort(unsorted_list):
"""
>>> unsorted_list = [5, 9, 2, 99, 3, 128, 3]
>>> insertion_sort(unsorted_list)
>>> unsorted_list
[2, 3, 3, 5, 9, 99, 128]
"""
for i in range(1, len(unsorted_list)):
shift(unsorted_list, i)
def shift(unsorted_list, index):
for i in range(0, index):
if unsorted_list[index] < unsorted_list[i]:
unsorted_list[index], unsorted_list[i] = unsorted_list[i], unsorted_list[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