javascript 快速排序
/*
快速排序
*/
var arr1=[1,5,6,3,9,4,8];
function quickSort(arr) {
if (arr.length<=1) {
return arr;
}
var pivotIndex = Math.floor(arr.length/2);
var jz = arr.splice(pivotIndex,1)[0];//截取字符串作为基准
var left = [];
var right = [];
for (var i = 0; i<arr.length; i++) {
if (arr[i]<jz) {
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quickSort(left).concat([jz],quickSort(right));
}
var ff = quickSort(arr1);
for(var i =0 ;i<ff.length;i++){
console.log(ff[i])
}
/*
考虑的问题 递归调用停止的条件,否则是无限循环
*/