Skip to content

Instantly share code, notes, and snippets.

@ShamsAnsari
Last active April 15, 2020 20:07
Show Gist options
  • Save ShamsAnsari/56fde9989af6992f556b4bdd656fb6b3 to your computer and use it in GitHub Desktop.
Save ShamsAnsari/56fde9989af6992f556b4bdd656fb6b3 to your computer and use it in GitHub Desktop.
python insertion sort algorithm
def insertion_sort(arr: []):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment