Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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