var arr = [3,2,4,1];
function merge(left, right) {
var tmp = [];
while (left.length && right.length) {
if (left[0] < right[0]) {
tmp.push(left.shift());
} else {
tmp.push(right.shift());
}
}
return tmp.concat(left, right);
}
function mergeSort(a) {
if (a.length == 1) {
return a;
}
var midIndex = ~~(a.length / 2);
var left = a.slice(0, midIndex);
var right = a.slice(midIndex);
return merge(mergeSort(left), mergeSort(right));
}
console.log(mergeSort(arr));