Skip to content

Instantly share code, notes, and snippets.

@dheshanm
Last active December 13, 2017 08:41
Show Gist options
  • Save dheshanm/b09dbab78a61c560cc290ed95b133bb0 to your computer and use it in GitHub Desktop.
Save dheshanm/b09dbab78a61c560cc290ed95b133bb0 to your computer and use it in GitHub Desktop.
procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
/* select value to be inserted */
valueToInsert = A[i]
holePosition = i
/*locate hole position for the element to be inserted */
while holePosition > 0 and A[holePosition-1] > valueToInsert do:
A[holePosition] = A[holePosition-1]
holePosition = holePosition -1
end while
/* insert the number at hole position */
A[holePosition] = valueToInsert
end for
end procedure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment