Skip to content

Instantly share code, notes, and snippets.

@lidio601
Created August 9, 2015 05:34
Show Gist options
  • Save lidio601/e412873d73058fc3d34e to your computer and use it in GitHub Desktop.
Save lidio601/e412873d73058fc3d34e to your computer and use it in GitHub Desktop.
Sample implementation of the Insert Sort algorithm ( O(N^2/4) order of complexity )
import random
__author__ = 'fabiocigliano'
def genarray():
n = int(random.random() * 10) + 1
ris = range(n)
random.shuffle(ris)
return ris
def scambia(array, i, j):
temp = array[i]
array[i] = array[j]
array[j] = temp
def insert_sort(a):
for i in range(1, len(a)):
for j in range(i):
if a[i] < a[j]:
scambia(a, i, j)
return array
if __name__ == "__main__":
array = genarray()
print "test input array: ", array
array = insert_sort(array)
print "test output array:", array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment