Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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