Created
June 19, 2018 08:16
-
-
Save xuhang57/c898ffff2f9510158e1c35113bc7624e to your computer and use it in GitHub Desktop.
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 i in range(1, len(arr)): | |
cur_val = arr[i] | |
# indexes of the previous values | |
j = i-1 | |
while j>=0 and cur_val < arr[j]: | |
arr[j+1] = arr[j] | |
j -= 1 | |
arr[j+1] = cur_val | |
return arr | |
insertion_sort([12,4,5,6,7,3,1,15]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment