Skip to content

Instantly share code, notes, and snippets.

@rafaelcaricio
Created September 11, 2012 11:55
Show Gist options
  • Save rafaelcaricio/3697861 to your computer and use it in GitHub Desktop.
Save rafaelcaricio/3697861 to your computer and use it in GitHub Desktop.
Insertion sort
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def insertion_sort(array):
size = len(array)
if size > 1:
for i in xrange(size):
j = i
while j:
if array[j-1] > array[j]:
aux = array[j-1]
array[j-1] = array[j]
array[j] = aux
else:
break
j -= 1
return array
if __name__ == '__main__':
assert [1,2] == insertion_sort([2,1])
assert [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] == insertion_sort([2, 0, 3, 7, 9, 8, 1, 4, 5, 6])
print insertion_sort(map(int, sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment