快速排序就是找基准,小的放左边,大的放右边,并把左右递归
var arr = [1,8,2,5,14,2,4,8,10,32,45,3];
console.log(sort(arr, "min"))
console.log(sort(arr, "max"))
function sort(arr, type) {
var arr_len = arr.length;
var arr1 = [];
arr1 = arr1.concat(arr);
var t = type ? (type.toLowerCase() === "min" ? true : false) : false;
return check(arr1);
function check(arr1) {
var arr_len = arr1.length;
if (arr_len <= 1){
return arr1;
}
var middle = parseInt(arr_len / 2);
var m = arr1.splice(middle, 1)[0];
var left = [];
var right = [];
arr_len--;
for (var i = 0; i < arr_len; i++){
var flag = m < arr1[i] ;
if (t) {
flag = m > arr1[i]
}
if (flag){
left.push(arr1[i]);
} else {
right.push(arr1[i]);
}
}
return check(left).concat([m], check(right));
}
}