mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 19:34:27 -05:00
adding soaked method calls, with caching
This commit is contained in:
11
examples/computer_science/bubble_sort.coffee
Normal file
11
examples/computer_science/bubble_sort.coffee
Normal file
@@ -0,0 +1,11 @@
|
||||
# A bubble sort implementation, sorting the given array in-place.
|
||||
bubble_sort: list =>
|
||||
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.
|
||||
print(bubble_sort([3, 2, 1]).join(' ') is '1 2 3')
|
||||
print(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')
|
||||
Reference in New Issue
Block a user