forked from xueliqq/shadowsocks-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.js
35 lines (31 loc) · 868 Bytes
/
merge_sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);