javascript实现快速排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'use strict';
/* jshint node: true */

const len = 100;
const ary = new Array(len).fill(0).map(() => Math.floor(Math.random() * len));

let ret = (function fastsort(ary) {
if (ary.length <= 1) return ary;

let k = Math.floor(Math.random() * ary.length);
let l_ary = ary.filter((item, index) => (index != k) && (item < ary[k]));
let r_ary = ary.filter((item, index) => (index != k) && (item >= ary[k]));

return fastsort(l_ary).concat(ary[k]).concat(fastsort(r_ary));
}(ary));

console.log(ary);
console.log(ret);
文章目录
,