Skip to content

Instantly share code, notes, and snippets.

@gatarelib
Created October 8, 2018 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatarelib/2a9627ce5c220ee73850adc17febc391 to your computer and use it in GitHub Desktop.
Save gatarelib/2a9627ce5c220ee73850adc17febc391 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment