PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/computer_science/bubble_sort.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 11 lines | 7 code | 2 blank | 2 comment | 2 complexity | a914ed66b98ffe0e05d1fb23d6074612 MD5 | raw file
 1# A bubble sort implementation, sorting the given array in-place.
 2bubble_sort = (list) ->
 3  for i in [0...list.length]
 4    for j in [0...list.length - i] when list[j] > list[j + 1]
 5      [list[j], list[j+1]] = [list[j + 1], list[j]]
 6  list
 7
 8
 9# Test the function.
10console.log bubble_sort([3, 2, 1]).join(' ') is '1 2 3'
11console.log bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'