Created
September 11, 2012 11:55
-
-
Save rafaelcaricio/3697861 to your computer and use it in GitHub Desktop.
Insertion sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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