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

相关推荐

码农索隆:以下是我以我微薄的认知提供的建议: 1.考个教师资格证,去当体育考试。 2.去健身房当健身教练(因为在我印象里面体育生身材都不错)。
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务