Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created June 5, 2018 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shameemreza/b7a4aeab204e72cf6717099c6941307d to your computer and use it in GitHub Desktop.
Save shameemreza/b7a4aeab204e72cf6717099c6941307d to your computer and use it in GitHub Desktop.
# Python program for implementation of Insertion Sort
# Function to do insertion sort
def insertionSort(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
# Driver code to test above
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment