recursive.js (567B)
1 function mergeSort(array) { 2 if(array.length < 2) 3 return array; 4 var middle = array.length >> 1; 5 var left = array.slice(0, middle); 6 var right = array.slice(middle); 7 8 return merge(mergeSort(left), mergeSort(right)); 9 } 10 11 function merge(left, right) { 12 var result = []; 13 while (left.length > 0 && right.length > 0) 14 result.push(left[0] < right[0]? left.shift() : right.shift()); 15 return result.concat(left.length? left : right); 16 } 17 // remember the function call should be SortedArr = mergeSort(arr); 18 // mergeSort(arr) would do no change to arr