|
|
|
@ -8,38 +8,38 @@
|
|
|
|
|
class QuickSort {
|
|
|
|
|
/* 元素交换 */
|
|
|
|
|
swap(nums, i, j) {
|
|
|
|
|
let tmp = nums[i]
|
|
|
|
|
nums[i] = nums[j]
|
|
|
|
|
nums[j] = tmp
|
|
|
|
|
let tmp = nums[i];
|
|
|
|
|
nums[i] = nums[j];
|
|
|
|
|
nums[j] = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哨兵划分 */
|
|
|
|
|
partition(nums, left, right){
|
|
|
|
|
partition(nums, left, right) {
|
|
|
|
|
// 以 nums[left] 作为基准数
|
|
|
|
|
let i = left, j = right
|
|
|
|
|
while(i < j){
|
|
|
|
|
while(i < j && nums[j] >= nums[left]){
|
|
|
|
|
j -= 1 // 从右向左找首个小于基准数的元素
|
|
|
|
|
let i = left, j = right;
|
|
|
|
|
while (i < j) {
|
|
|
|
|
while (i < j && nums[j] >= nums[left]) {
|
|
|
|
|
j -= 1; // 从右向左找首个小于基准数的元素
|
|
|
|
|
}
|
|
|
|
|
while(i < j && nums[i] <= nums[left]){
|
|
|
|
|
i += 1 // 从左向右找首个大于基准数的元素
|
|
|
|
|
while (i < j && nums[i] <= nums[left]) {
|
|
|
|
|
i += 1; // 从左向右找首个大于基准数的元素
|
|
|
|
|
}
|
|
|
|
|
// 元素交换
|
|
|
|
|
this.swap(nums, i, j) // 交换这两个元素
|
|
|
|
|
this.swap(nums, i, j); // 交换这两个元素
|
|
|
|
|
}
|
|
|
|
|
this.swap(nums, i, left) // 将基准数交换至两子数组的分界线
|
|
|
|
|
return i // 返回基准数的索引
|
|
|
|
|
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
|
|
|
|
return i; // 返回基准数的索引
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 快速排序 */
|
|
|
|
|
quickSort(nums, left, right){
|
|
|
|
|
quickSort(nums, left, right) {
|
|
|
|
|
// 子数组长度为 1 时终止递归
|
|
|
|
|
if(left >= right) return
|
|
|
|
|
if (left >= right) return;
|
|
|
|
|
// 哨兵划分
|
|
|
|
|
const pivot = this.partition(nums, left, right)
|
|
|
|
|
const pivot = this.partition(nums, left, right);
|
|
|
|
|
// 递归左子数组、右子数组
|
|
|
|
|
this.quickSort(nums, left, pivot - 1)
|
|
|
|
|
this.quickSort(nums, pivot + 1, right)
|
|
|
|
|
this.quickSort(nums, left, pivot - 1);
|
|
|
|
|
this.quickSort(nums, pivot + 1, right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -47,21 +47,18 @@ class QuickSort {
|
|
|
|
|
class QuickSortMedian {
|
|
|
|
|
/* 元素交换 */
|
|
|
|
|
swap(nums, i, j) {
|
|
|
|
|
let tmp = nums[i]
|
|
|
|
|
nums[i] = nums[j]
|
|
|
|
|
nums[j] = tmp
|
|
|
|
|
let tmp = nums[i];
|
|
|
|
|
nums[i] = nums[j];
|
|
|
|
|
nums[j] = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 选取三个元素的中位数 */
|
|
|
|
|
medianThree(nums, left, mid, right) {
|
|
|
|
|
// 使用了异或操作来简化代码
|
|
|
|
|
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
|
|
|
|
if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right]))
|
|
|
|
|
return left;
|
|
|
|
|
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
|
|
|
|
return mid;
|
|
|
|
|
else
|
|
|
|
|
return right;
|
|
|
|
|
if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right])) return left;
|
|
|
|
|
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid;
|
|
|
|
|
else return right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哨兵划分(三数取中值) */
|
|
|
|
@ -73,14 +70,12 @@ class QuickSortMedian {
|
|
|
|
|
// 以 nums[left] 作为基准数
|
|
|
|
|
let i = left, j = right;
|
|
|
|
|
while (i < j) {
|
|
|
|
|
while (i < j && nums[j] >= nums[left])
|
|
|
|
|
j--; // 从右向左找首个小于基准数的元素
|
|
|
|
|
while (i < j && nums[i] <= nums[left])
|
|
|
|
|
i++; // 从左向右找首个大于基准数的元素
|
|
|
|
|
while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素
|
|
|
|
|
while (i < j && nums[i] <= nums[left]) i++; // 从左向右找首个大于基准数的元素
|
|
|
|
|
this.swap(nums, i, j); // 交换这两个元素
|
|
|
|
|
}
|
|
|
|
|
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
|
|
|
|
return i; // 返回基准数的索引
|
|
|
|
|
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
|
|
|
|
return i; // 返回基准数的索引
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 快速排序 */
|
|
|
|
@ -99,9 +94,9 @@ class QuickSortMedian {
|
|
|
|
|
class QuickSortTailCall {
|
|
|
|
|
/* 元素交换 */
|
|
|
|
|
swap(nums, i, j) {
|
|
|
|
|
let tmp = nums[i]
|
|
|
|
|
nums[i] = nums[j]
|
|
|
|
|
nums[j] = tmp
|
|
|
|
|
let tmp = nums[i];
|
|
|
|
|
nums[i] = nums[j];
|
|
|
|
|
nums[j] = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哨兵划分 */
|
|
|
|
@ -109,13 +104,11 @@ class QuickSortTailCall {
|
|
|
|
|
// 以 nums[left] 作为基准数
|
|
|
|
|
let i = left, j = right;
|
|
|
|
|
while (i < j) {
|
|
|
|
|
while (i < j && nums[j] >= nums[left])
|
|
|
|
|
j--; // 从右向左找首个小于基准数的元素
|
|
|
|
|
while (i < j && nums[i] <= nums[left])
|
|
|
|
|
i++; // 从左向右找首个大于基准数的元素
|
|
|
|
|
while (i < j && nums[j] >= nums[left]) j--; // 从右向左找首个小于基准数的元素
|
|
|
|
|
while (i < j && nums[i] <= nums[left]) i++; // 从左向右找首个大于基准数的元素
|
|
|
|
|
this.swap(nums, i, j); // 交换这两个元素
|
|
|
|
|
}
|
|
|
|
|
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
|
|
|
|
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
|
|
|
|
return i; // 返回基准数的索引
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -127,8 +120,8 @@ class QuickSortTailCall {
|
|
|
|
|
let pivot = this.partition(nums, left, right);
|
|
|
|
|
// 对两个子数组中较短的那个执行快排
|
|
|
|
|
if (pivot - left < right - pivot) {
|
|
|
|
|
this.quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
|
|
|
|
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
|
|
|
|
this.quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
|
|
|
|
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
|
|
|
|
} else {
|
|
|
|
|
this.quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
|
|
|
|
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
|
|
|
@ -139,19 +132,19 @@ class QuickSortTailCall {
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
/* 快速排序 */
|
|
|
|
|
var nums = [4, 1, 3, 1, 5, 2]
|
|
|
|
|
var quickSort = new QuickSort()
|
|
|
|
|
quickSort.quickSort(nums, 0, nums.length - 1)
|
|
|
|
|
console.log("快速排序完成后 nums =", nums)
|
|
|
|
|
const nums = [4, 1, 3, 1, 5, 2];
|
|
|
|
|
const quickSort = new QuickSort();
|
|
|
|
|
quickSort.quickSort(nums, 0, nums.length - 1);
|
|
|
|
|
console.log('快速排序完成后 nums =', nums);
|
|
|
|
|
|
|
|
|
|
/* 快速排序(中位基准数优化) */
|
|
|
|
|
nums1 = [4, 1, 3, 1, 5,2]
|
|
|
|
|
var quickSortMedian = new QuickSort()
|
|
|
|
|
quickSortMedian.quickSort(nums1, 0, nums1.length - 1)
|
|
|
|
|
console.log("快速排序(中位基准数优化)完成后 nums =", nums1)
|
|
|
|
|
const nums1 = [4, 1, 3, 1, 5, 2];
|
|
|
|
|
const quickSortMedian = new QuickSort();
|
|
|
|
|
quickSortMedian.quickSort(nums1, 0, nums1.length - 1);
|
|
|
|
|
console.log('快速排序(中位基准数优化)完成后 nums =', nums1);
|
|
|
|
|
|
|
|
|
|
/* 快速排序(尾递归优化) */
|
|
|
|
|
nums2 = [4, 1, 3, 1, 5, 2]
|
|
|
|
|
var quickSortTailCall = new QuickSort()
|
|
|
|
|
quickSortTailCall.quickSort(nums2, 0, nums2.length - 1)
|
|
|
|
|
console.log("快速排序(尾递归优化)完成后 nums =", nums2)
|
|
|
|
|
const nums2 = [4, 1, 3, 1, 5, 2];
|
|
|
|
|
const quickSortTailCall = new QuickSort();
|
|
|
|
|
quickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
|
|
|
|
|
console.log('快速排序(尾递归优化)完成后 nums =', nums2);
|
|
|
|
|