Skip to content

Instantly share code, notes, and snippets.

@xuhang57
Created June 19, 2018 08:16
Show Gist options
  • Save xuhang57/c898ffff2f9510158e1c35113bc7624e to your computer and use it in GitHub Desktop.
Save xuhang57/c898ffff2f9510158e1c35113bc7624e to your computer and use it in GitHub Desktop.
Insertion Sort
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