<script type="text/javascript" >
function quickSort(ary) {
// 找到基准点的索引
if(ary.length<=1){
return ary ;
}
var pointIndex = Math.floor(ary.length/2);
// 通过基准点的索引在原来的数组中,删除这一项,并把基准点这一项的值获取到
var pointValue = ary.splice(pointIndex,1)[0];
var left=[];
var right=[];
for(var i=0;i<ary.length;i++) {
var cur = ary[i];
cur < pointValue ? left.push(cur) : right.push(cur);
}
//重新把 左中右 组合到一起
return quickSort(left).concat([pointValue],quickSort(right));
}
var ary = [12,13,23,14,20,26,34,13,16];
var res = quickSort(ary);
console.log(res);
</script>
网友评论