function mergeSort(array){
if(array.length==1){
return array;
}
for(let len = 2;len/2<=array.length;len*=2){
for(let start=0;start<array.length;start+=len){
sort(start,len,array)
}
}
return array;
}
function sort(start,len,arr){
let middle = start+ len/2;
let left = arr.slice(start,middle);
let right = arr.slice(middle,start+len);
let res = [];
while(left.length>0 && right.length>0){
if(left[0]<right[0]){
res.push(left[0])
left.shift();
}else{
res.push(right[0]);
right.shift();
}
}
let replace = res.concat(left).concat(right);
arr.splice(start,len,...replace);
}
let a = [3,1,0,9,8,11,12,6,4,7]
var result = mergeSort(a);
console.log("arr:",result);
1
guyeu 2019-07-13 14:27:24 +08:00
算。
|
2
doing1 2019-07-13 14:29:16 +08:00
是的
|
3
singleQ 2019-07-13 23:21:40 +08:00
这个是快牌
|