2010-01-24 17:52:15 +00:00
|
|
|
# A bubble sort implementation, sorting the given array in-place.
|
2010-07-29 04:03:42 +00:00
|
|
|
bubble_sort = (list) ->
|
2010-10-25 01:37:27 +00:00
|
|
|
for i from 0 to list.length - 1
|
|
|
|
for j from 0 to list.length - i - 1
|
2010-07-29 04:03:42 +00:00
|
|
|
[list[j], list[j+1]] = [list[j+1], list[j]] if list[j] > list[j+1]
|
2010-01-24 17:52:15 +00:00
|
|
|
list
|
|
|
|
|
|
|
|
|
|
|
|
# Test the function.
|
2010-10-24 16:48:42 +00:00
|
|
|
console.log bubble_sort([3, 2, 1]).join(' ') is '1 2 3'
|
|
|
|
console.log bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'
|