<script type="text/javascript">
function quickSort(arr){
if(arr.length<=1){
return arr;//如果数组只有一个数,就直接返回;
}
var num = Math.floor(arr.length/2);//取中间数,如果不是整数,就向下取整
var munValue = arr.splice(num,1)
var left = [];
var right = [];
for(let i = 0;i<arr.length;i++){
if(arr[i]<numValue){
left.push(arr[i]); //基准点的左边的数传到左边数组
}else{
right.push(arr[i]);//基准点的右边的数传到右数组
}
}
return quickSort(left).concat([numValue],quickSort(right));
//递归不断重复比较
}
console.log(quickSort([32,45,37,16,2,87]));
//2,16,32,37,45,87
</script>
04-19
6596
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-17
7057
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)