Skip to content

Instantly share code, notes, and snippets.

@MorrisLaw
Last active August 10, 2017 17:08
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 MorrisLaw/575d0f59ed4e8415869545f25e5300f1 to your computer and use it in GitHub Desktop.
Save MorrisLaw/575d0f59ed4e8415869545f25e5300f1 to your computer and use it in GitHub Desktop.
Insertion sort, ascending order based off of the pseudocode in Introduction to Algorithms by C.L.R.S.
A = [10,2,5,7,3,22]
for j in xrange(1, len(A)):
key = A[j]
# Insert A[j] into the sorted sequence A[1..j-1].
i = j - 1
while (i > -1 and A[i] > key):
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
print(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment