Sha256: 2f5bdd7b609211c6dbec48fff83b47170ecbf10eeff744088f5975aa92f1735c
Contents?: true
Size: 551 Bytes
Versions: 10
Compression:
Stored size: 551 Bytes
Contents
# Sorts an array in ascending natural order using merge sort. merge_sort = (list) -> return list if list.length is 1 result = [] pivot = Math.floor list.length / 2 left = merge_sort list.slice 0, pivot right = merge_sort list.slice pivot while left.length and right.length result.push(if left[0] < right[0] then left.shift() else right.shift()) result.concat(left).concat(right) # Test the function. console.log merge_sort([3, 2, 1]).join(' ') is '1 2 3' console.log merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'
Version data entries
10 entries across 10 versions & 2 rubygems