Skip to content

Instantly share code, notes, and snippets.

@gatarelib
Last active October 8, 2018 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatarelib/f6392bbc0116d456963c920790e8108b to your computer and use it in GitHub Desktop.
Save gatarelib/f6392bbc0116d456963c920790e8108b to your computer and use it in GitHub Desktop.
insertion_sort Algo created by ligat - https://repl.it/@ligat/insertionsort-Algo
# Insertion Sort In Python
#
# Performance Complexity = O(n^2)
# Space Complexity = O(n)
def insertionSort(my_list):
# for every element in our array
for index in range(1, len(my_list)):
current = my_list[index]
position = index
while position > 0 and my_list[position-1] > current:
print("Swapped {} for {}".format(my_list[position], my_list[position-1]))
my_list[position] = my_list[position-1]
print(my_list)
position -= 1
my_list[position] = current
return my_list
my_list = [8,2,1,3,5,4]
print(insertionSort(my_list))
@gatarelib
Copy link
Author

Program to illustrate insertion Sort Algorithm i Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment