js归并排序实现
let arr = [3,4,2,8,1,1,2,2,3,3,4,5];
const merge = function(L,M,R) {
let arr2=[];
let i=0;
let p1 = L;
let p2 = M+1;
while(p1<=M&&p2<=R) {
arr2[i++] = arr[p1]>=arr[p2]?arr[p1++]:arr[p2++];
}
while(p1<=M) {
arr2[i++] = arr[p1++];
}
while(p2<=R) {
arr2[i++] = arr[p2++];
}
for(i =0;i<arr2.length;i++) {
arr[L+i] = arr2[i];
}
}
const mergeSort = function(L,R) {
if(L === R) {
return;
}
let mid = L + ((R-L)>>1);
mergeSort(L,mid);
mergeSort(mid+1,R)
return merge(L,mid,R);
}