Last active
August 29, 2015 14:15
-
-
Save rcanepa/236e1af93969e665268f to your computer and use it in GitHub Desktop.
Python Insertion Sort
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
def insertion_sort(arr): | |
for idx, number in enumerate(arr): | |
current = idx | |
previous = idx - 1 | |
if idx > 0: | |
while arr[current] < arr[previous] and (previous >= 0): | |
temp = arr[current] | |
arr[current] = arr[previous] | |
arr[previous] = temp | |
current = previous | |
previous -= 1 | |
return arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment