Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created August 6, 2008 10:07
Show Gist options
  • Select an option

  • Save ishikawa/4201 to your computer and use it in GitHub Desktop.

Select an option

Save ishikawa/4201 to your computer and use it in GitHub Desktop.
def insertion_sort(lst):
"""
>>> insertion_sort([3, 4, 2, 10, 1])
[1, 2, 3, 4, 10]
>>> insertion_sort([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
>>> insertion_sort([])
[]
"""
for i in xrange(1, len(lst)):
tmp = lst[i]
j = i
while j > 0 and lst[j - 1] > tmp:
lst[j] = lst[j - 1]
j -= 1
lst[j] = tmp
return lst
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment