2010-01-24 12:52:15 -05:00
|
|
|
# A bubble sort implementation, sorting the given array in-place.
|
2010-01-26 10:52:05 -05:00
|
|
|
bubble_sort: (list) ->
|
2010-01-24 12:52:15 -05:00
|
|
|
for i in [0...list.length]
|
|
|
|
for j in [0...list.length - i]
|
|
|
|
[list[j], list[j+1]]: [list[j+1], list[j]] if list[j] > list[j+1]
|
|
|
|
list
|
|
|
|
|
|
|
|
|
|
|
|
# Test the function.
|
2010-01-29 23:30:54 -05:00
|
|
|
puts(bubble_sort([3, 2, 1]).join(' ') is '1 2 3')
|
|
|
|
puts(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
|