function mergeSort(array) {
function sort(array, first, last) {
first = (first === undefined) ? 0 : first
last = (last === undefined) ? array.length - 1 : last
if (last - 1 < first) {
return;
}
var middle = Math.floor((first + last) / 2);
sort(array, first, middle);
sort(array, middle + 1, last);
merge(array, first, middle, last);
}
function merge(array, first, middle, last) {
var f = first;
var m = middle;
var i;
var temp;
while (f <= m && m + 1 <= last) {
if (array[f] >= array[m + 1]) { // 这里使用了插入排序的思想
temp = array[m + 1];
for (i = m; i >= f; i--) {
array[i + 1] = array[i];
}
array[f] = temp;
m++
} else {
f++
}
}
return array;
}
sort(array);
}