×

用户注册

已有账号? 登录
algorithm

cloud-cola

2020-11-14 21:25

快速排序算法 222

var arr = [4,1,6,9,3,2,8,7];

function quicksort(arr){
if(arr == null || arr.length==0){
return [];
}
var leader = arr[0];
var left = [];
var right = [];
for(let i = 1;i<arr.length;i++){
if(arr[i] < leader) {left.push(arr[i])
}else {right.push(arr[i])}

}
left = quicksort(left);
right = quicksort(right)

left.push(leader);
return left.concat(right);
}
console.log(quicksort(arr));


参考:

https://www.bilibili.com/video/BV1at411T75o

暂无任何评论