这是我照着书上敲的归并排序,可是控制台输出merg函数while判断中的length为undefined
function splitMergeSort(arr){
var len = arr.length;
while(len == 1){
return arr;
}
var mid = Math.floor(len/2);
var left = arr.slice(0, mid),
right = arr.slice(mid, len);
return splitMergeSort(merge(left),merge(right))
}
//合并函数
function merge(arr1,arr2){
var result = [];
var l = 0;
var r = 0;
while(l<arr1.length && r<arr2.length){
if(arr1[l]<arr2[r]){
result.push(arr1[l++])
}else{
result.push(arr2[r++])
}
}
while(l<arr1.length){
result.push(arr1[l++])
}
while(r<arr2.length){
result.push(arr2[r++])
}
}
FFIVE
相关分类