不知道取什么名字名字: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));
0 点赞 评论 收藏
分享
关注他的用户也关注了: