File: bubble_sort.coffee

package info (click to toggle)
coffeescript 1.10.0~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,292 kB
  • sloc: makefile: 62
file content (11 lines) | stat: -rw-r--r-- 384 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
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] when list[j] > list[j + 1]
      [list[j], list[j+1]] = [list[j + 1], list[j]]
  list


# Test the function.
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'