标签:math 快速排序算法 log add return mat concat index turn
function quickSort(arr,dir) { var dir=dir||‘add‘; if (arr.length <= 1) { return arr; } var pivotIndex = Math.floor(arr.length / 2) ; var pivot = arr.splice(pivotIndex, 1)[0]; //基准 var left = []; var right = []; if(dir==‘add‘){ //生序 for (var i = 0; i < arr.length; i++){ arr[i] < pivot ? left.push(arr[i]): right.push(arr[i]); } }else{ for (var i = 0; i < arr.length; i++){ arr[i] > pivot ? left.push(arr[i]): right.push(arr[i]); } } return quickSort(left).concat([pivot], quickSort(right)) //不断递归 }; var arr=[3,2,67,10,345]; console.log(quickSort(arr,‘aa‘))
标签:math 快速排序算法 log add return mat concat index turn
原文地址:http://www.cnblogs.com/wanghaonull/p/6259864.html