快速排序
2017-03-18 本文已影响0人
且听风吟_8d10
<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>