js快速排序

时间:2022-04-01 08:14:17
function sort(arr){
if(arr.length<=1){
return arr;
}
var num = Math.floor(arr.length/2);
var numValue = arr.splice(num,1);
var left = [];
var right = [];
for(var i = 0; i < arr.length; i++){
if(arr[i]<numValue){
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return sort(left).concat([numValue],sort(right));
} alert(sort([12,6,5,37,40]));