1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| function mergeSort(arr) { var len = arr.length; if(len < 2) { return arr; } var middle = Math.floor(len / 2), left = arr.slice(0, middle), right = arr.slice(middle); return merge(mergeSort(left), mergeSort(right)); }
function merge(left, right){ var result = [], il = 0, ir = 0 while(il < left.length && ir < right.length){ if( left[il] < right[ir] ){ result.push(left[il++]) }else{ result.push[right[ir++]] } } while(left[il]){ result.push(left[il++]) } while(right[ir]){ result.push(right[ir++]) } return result }
|