function merge(arr1,arr2){
let result = [];
let i=0;
let j=0;
while(i<arr1.length&&j<arr2.length){
console.log('i='+i+''+'j='+j);
if(arr2[j]>arr1[i]){
result.push(arr1[i]);
i++;
console.log(result);
}else{
result.push(arr2[j]);
j++;
console.log(result);
}
}
while(i<arr1.length){
result.push(arr1[i]);
i++;
}
while(j<arr2.length){
result.push(arr2[j]);
j++;
}
return result;
}
function mergeSort(arr){
if(arr.length<=1) return arr;
let mid = Math.floor(arr.length/2);
let left = mergeSort(arr.slice(0,mid));
let right = mergeSort(arr.slice(mid));
return merge(left,right);
}
console.log(mergeSort([5,60,1,20,3,44,21]));