function quickSort(arr) { if (arr.length <= 1) { return arr; } let pivotIndex; if (arr.length % 2 ===0) { pivotIndex = Math.floor(arr.length / 2 - 1); }else { pivotIndex = Math.floor(arr.length / 2); } let pivot = arr[pivotIndex]; let left = []; let right = []; for (let i = 0; i < pivotIndex; i++) { if (arr[i] < pivot) { left.push(arr[i]); } else { right.push(arr[i]); } } for (let j = pivotIndex + 1; j < arr.length; j++) { if (arr[j] < pivot) { left.push(arr[j]); }else { right.push(arr[j]); } } return quickSort(left).concat(pivot,quickSort(right)); } const arr = [90,3,2,1,5,8,9,20,40,21,3]; console.log(quickSort(arr));
点赞 4

相关推荐

06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务