二路归并排序
var arr = [49, 38, 65, 97, 76, 13, 27, 48, 55, 4]; function MergeSort(arr) { var len = arr.length; if(len<=1){ return arr; } var middle = Math.floor(len/2); var left = arr.slice(0,middle); var right = arr.slice(middle) return merge(MergeSort(left),MergeSort(right)); } function merge(left,right){ var result = []; while(left.length && right.length){ if(left[0]<=right[0]){ result.push(left.shift()) }else{ result.push(right.shift()) } } while(left.length){ result.push(left.shift()) } while(right.length){ result.push(right.shift()) } return result; } console.log(MergeSort(arr)); //4, 13, 27, 38, 48, 49, 55, 65, 76, 97
排序算法 文章被收录于专栏
排序算法