Sort
又爱又恨的排序方法
快速排序
主要思路是找一个标志位,将标志位左右两边都排序
var sortArray = function(nums) {
quickSort(nums, 0, nums.length-1)
return nums
};
function quickSort(arr, start, end) {
if(start < end) {
const pivot = partition(arr, start, end)
quickSort(arr, start, pivot - 1)
quickSort(arr, pivot + 1, end)
}
}
function partition(arr, start, end) {
const pivot = arr[start]
let i=start, j=end
while(i < j) {
while(i < j && arr[j] >= pivot)
j--
arr[i] = arr[j]
while(i < j && arr[i] < pivot)
i++
arr[j] = arr[i]
}
arr[i] = pivot
return i
}Updated on 6/20/2023