## 归并排序 原

漂泊者及其影子

``````console.log("锋叔子的归并排序");

function merge(left, right) {
var result = [];
while (left.length > 0 && right.length > 0) {
if (left[0] < right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}
result = result.concat(left).concat(right);
console.log(result);
return result;
}

function mergeSort(items) {
if (items.length === 1) {
return items;
}
var middle = Math.floor(items.length / 2);
var left = items.slice(0, middle);
var right = items.slice(middle);
//递归调用mergeSort，如果返回的不是结果组，而是一个函数，则继续调用
return merge(mergeSort(left), mergeSort(right));
}

//测试排序
function testSort() {
var myArray = initArray();
var tempArray = [];
var myArrayAfterSort = mergeSort(myArray);
for (var i = 0; i < myArrayAfterSort.length; i++) {
console.log(myArrayAfterSort[i]);
}
}
//初始化数组
function initArray()
{
var myArray = [9, 3, 7, 8, 4, 6, 77, 88];
return myArray;
}

testSort();``````

### 漂泊者及其影子

Lubby
2015/10/08
102
0
java泛型中使用的排序算法——归并排序及分析

9龙
04/29
0
0

dby_freedom
02/26
0
0

2016/04/07
84
0

2012/12/10
58
0

24分钟前
2
0

27分钟前
3
0

Tronlong创龙
39分钟前
3
0

aiChuang
39分钟前
3
0
McDonald’s is using Alexa and Google to accepting job applications

McDonald’s today announced a new initiative the fast food chain is calling the “Apply Thru,” in which owners of Amazon Alexa or Google Assistant devices can begin job applic......

wowloop
43分钟前
4
0