Last active
October 8, 2018 05:58
-
-
Save gatarelib/f6392bbc0116d456963c920790e8108b to your computer and use it in GitHub Desktop.
insertion_sort Algo created by ligat - https://repl.it/@ligat/insertionsort-Algo
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
# 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
Program to illustrate insertion Sort Algorithm i Python