一谈,原始的疾速排序
function swap(arr, i, j) {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
function quickSort(arr, fromIndex, length) {
if (length < 2) {
return
}
// arr[midIndex] 的位置已牢固,不用在排
let midIndex = partition(arr, fromIndex, length)
let subLength = midIndex - fromIndex
quickSort(arr, fromIndex, subLength)
if (midIndex + 1 !== arr.length) {
quickSort(arr, midIndex + 1, length - subLength - 1)
}
}
function partition(arr, fromIndex, length) {
let lastIndex = fromIndex + length - 1
let pivot = arr[lastIndex]
let lastIndexUnderPivot = fromIndex - 1
for (let currentIndex = fromIndex; currentIndex < lastIndex; currentIndex++) {
if (arr[currentIndex] <= pivot) {
swap(arr, lastIndexUnderPivot + 1, currentIndex)
lastIndexUnderPivot++
}
}
swap(arr, lastIndexUnderPivot + 1, lastIndex)
return lastIndexUnderPivot + 1
}
let arr = [1, 5, 2, 11, 7, 3, 1, 6, 17, 10, 312, 312, 1, 1, 2323, 4, 56, 3, 14, 5543]
quickSort(arr, 0, arr.length)
console.log(arr) // [ 1, 1, 1, 1, 2, 3, 3, 4, 5, 6, 7, 10, 11, 14, 17, 56, 312, 312, 2323, 5543 ]
二谈,优化后的疾速排序
合时的采纳插入排序
代码略
随机化疾速排序
转变挑选主元 pivot 的体式格局,从挑选末端的元素,改成随机挑选
修正 partition 函数
function partition(arr, fromIndex, length) {
let lastIndex = fromIndex + length - 1
let randomIndex = fromIndex + Math.floor(Math.random() * length) // 随机索引
swap(arr, randomIndex, lastIndex) // 与末了一个元素交流,其他稳定
let pivot = arr[lastIndex]
let lastIndexUnderPivot = fromIndex - 1
for (let currentIndex = fromIndex; currentIndex < lastIndex; currentIndex++) {
if (arr[currentIndex] <= pivot) {
swap(arr, lastIndexUnderPivot + 1, currentIndex)
lastIndexUnderPivot++
}
}
swap(arr, lastIndexUnderPivot + 1, lastIndex)
return lastIndexUnderPivot + 1
}
三路快排
function swap(arr, i, j) {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
function quickSort(arr, fromIndex, length) {
if (length < 2) {
return
}
let [firstIndexEqualPivot, lastIndexEqualPivot] = partition(arr, fromIndex, length)
// pivot 都已排好序
quickSort(arr, fromIndex, firstIndexEqualPivot - fromIndex)
let subLength = fromIndex + length - 1 - (lastIndexEqualPivot + 1) + 1
// subLength = 末端索引 - 第一个大于主元素的索引 + 1
quickSort(arr, lastIndexEqualPivot + 1, subLength)
}
function partition(arr, fromIndex, length) {
let lastIndex = fromIndex + length - 1
let randomIndex = fromIndex + Math.floor(Math.random() * length)
swap(arr, randomIndex, lastIndex)
let pivot = arr[lastIndex]
let lastIndexUnderPivot = fromIndex - 1
let firstIndexOverPivot = lastIndex
let currentLeftIndex = fromIndex
let currentRightIndex = lastIndex - 1
while (currentLeftIndex <= currentRightIndex) {
while (arr[currentLeftIndex] <= pivot) {
if (arr[currentLeftIndex] < pivot) {
swap(arr, lastIndexUnderPivot + 1, currentLeftIndex)
lastIndexUnderPivot++
}
currentLeftIndex++
}
while (arr[currentRightIndex] >= pivot) {
if (arr[currentRightIndex] > pivot) {
swap(arr, firstIndexOverPivot - 1, currentRightIndex)
firstIndexOverPivot--
}
currentRightIndex--
}
// 越界
if (currentLeftIndex > lastIndex - 1) {
break
}
// 越界
if (currentRightIndex < fromIndex) {
break
}
// 越界
if (currentLeftIndex > currentRightIndex) {
break
}
// 此时arr[currentLeftIndex] > pivot, arr[currentRightIndex] < pivot
swap(arr, currentLeftIndex, currentRightIndex)
}
swap(arr, firstIndexOverPivot, lastIndex)
let firstIndexEqualPivot = lastIndexUnderPivot + 1
let lastIndexEqualPivot = firstIndexOverPivot
return [firstIndexEqualPivot, lastIndexEqualPivot]
}
let arr = [1, 5, 2, 11, 7, 3, 1, 6, 17, 10]
quickSort(arr, 0, arr.length)
console.log(arr) // [ 1, 2, 3, 5, 6, 1, 7, 10, 11, 17 ]
尾递归
套路和三谈合并排序(含尾递归)中的一样,将要用然则来不及用的参数存起来,在适宜的时刻,再用,这里的适宜平常都是盘算至恭弘=叶 恭弘结点的时刻
仅需修正 quickSort 函数以下,传入参数时,多个空数组
function quickSort(arr, fromIndex, length, argsArr) {
if (length < 2) {
if (argsArr.length === 0) {
return
}
let args = argsArr.pop()
return quickSort(arr, args[0], args[1], argsArr)
}
let [firstIndexEqualPivot, lastIndexEqualPivot] = partition(arr, fromIndex, length)
// pivot 都已排好序
argsArr.push([lastIndexEqualPivot + 1, fromIndex + length - 1 - (lastIndexEqualPivot + 1) + 1])
return quickSort(arr, fromIndex, firstIndexEqualPivot - fromIndex, argsArr)
}
...
let arr = [1, 5, 2, 11, 7, 3, 1, 6, 17, 10]
quickSort(arr, 0, arr.length, [])
console.log(arr) // [ 1, 2, 3, 5, 6, 1, 7, 10, 11, 17 ]
四谈,疾速排序的运用,top-k 题目
《算法导论》9.3 最坏状况为线性时候的挑选算法
输入是n个差别元素构成的数组,求第i小的元素,i从0算起
function insertSort(arr, fromIndex, length) {
for (let currentIndex = fromIndex + 1; currentIndex < fromIndex + length; currentIndex++) {
let currentCard = arr[currentIndex]
let j = currentIndex - 1
for (j; j >= fromIndex; j--) {
if (currentCard > arr[j]) {
break
} else {
arr[j + 1] = arr[j]
}
}
arr[j + 1] = currentCard
}
}
// 肯定n个差别元素的数组中,第i小的元素
function BFPRT(arr, flag) {
if (arr.length === 1) {
return arr[0]
}
let medianArr = []
for (let i = 0; i < arr.length; i = i + 5) {
if (i + 5 > arr.length) {
insertSort(arr, i, arr.length - i)
if ((arr.length - i) % 2 === 0) {
let index = (arr.length - i) / 2 + i - 1
medianArr.push(arr[index])
} else {
let index = Math.floor((arr.length - i) / 2) + i
medianArr.push(arr[index])
}
} else {
insertSort(arr, i, 5)
medianArr.push(arr[i + 2])
}
// 对每组元素举行插入排序,肯定中位数
}
let subFlag = 0
if (medianArr.length % 2 === 0) {
subFlag = medianArr.length / 2 - 1
} else {
subFlag = Math.floor(medianArr.length / 2)
}
let pivot = BFPRT(medianArr, subFlag)
let [leftArr, rightArr] = partition(arr, pivot)
if (leftArr.length === flag) {
return pivot
}
if (leftArr.length > flag) {
return BFPRT(leftArr, flag)
}
if (leftArr.length < flag) {
return BFPRT(rightArr, flag - leftArr.length - 1)
}
}
function partition(arr, pivot) {
let leftArr = []
let rightArr = []
for (let i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
leftArr.push(arr[i])
}
if (arr[i] > pivot) {
rightArr.push(arr[i])
}
}
return [leftArr, rightArr]
}
let arr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 11, 13]
console.log(BFPRT(arr, 2))