diff --git a/merge_sort.js b/merge_sort.js new file mode 100644 index 0000000..32d2554 --- /dev/null +++ b/merge_sort.js @@ -0,0 +1,35 @@ +// Generated by CoffeeScript 1.10.0 +(function() { + var merge, merge_sort; + + merge = function(left, right, comparison) { + var result; + result = new Array(); + while ((left.length > 0) && (right.length > 0)) { + if (comparison(left[0], right[0]) <= 0) { + result.push(left.shift()); + } else { + result.push(right.shift()); + } + } + while (left.length > 0) { + result.push(left.shift()); + } + while (right.length > 0) { + result.push(right.shift()); + } + return result; + }; + + merge_sort = function(array, comparison) { + var middle; + if (array.length < 2) { + return array; + } + middle = Math.ceil(array.length / 2); + return merge(merge_sort(array.slice(0, middle), comparison), merge_sort(array.slice(middle), comparison), comparison); + }; + + exports.merge_sort = merge_sort; + +}).call(this);