js数组快速排序

<script type="text/javascript">
var arr = [1, 2, 3, 54, 22, 1, 2, 3];
function quick(arr) {
if (arr.length <= 1)
return arr;
var proiindex = Math.floor(arr.length / 2);
var proift = arr.splice(proiindex, 1)[0]; //找基准,并把基准从原数组删除
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] <= proift) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quick(left).concat([proift], quick(right));
}
document.write(quick(arr));
</script>

上一篇:【Android】Handler详解


下一篇:alibaba/fescar 阿里巴巴 开源 分布式事务中间件