Skip to content

Instantly share code, notes, and snippets.

@island205
Created October 25, 2012 06:30
Show Gist options
  • Select an option

  • Save island205/3950852 to your computer and use it in GitHub Desktop.

Select an option

Save island205/3950852 to your computer and use it in GitHub Desktop.
insertSort = (arr)->
for i in [1...arr.length]
for j in [i-1..0]
if arr[j] > arr[j+1]
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
arr
#test
console.log insertSort [1,2,3,3,4,6, 9, 2, 1]
console.log insertSort [1]
console.log insertSort [0]
console.log insertSort [1,0]
console.log insertSort [1,0,1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment