Created
October 1, 2019 21:10
-
-
Save mauriciogardini/8786f1079e785b227aef79eac0c9aa4d to your computer and use it in GitHub Desktop.
Insertion Sort implementation in Python
This file contains hidden or 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(array, decrescent=False): | |
if len(array) <= 1: | |
return array | |
for x in range(1, len(array)): | |
y = x | |
while y > 0 and ((decrescent and array[y] > array[y - 1]) or | |
(not decrescent and array[y] < array[y - 1])): | |
array[y], array[y - 1] = array[y - 1], array[y] | |
y = y - 1 | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment