Created
August 9, 2015 05:34
-
-
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 )
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
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