|
|
|
@ -144,7 +144,30 @@ comments: true
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
|
|
|
|
/* 元素交换 */
|
|
|
|
|
function swap(nums: number[], i: number, j: number): void {
|
|
|
|
|
let tmp = nums[i];
|
|
|
|
|
nums[i] = nums[j];
|
|
|
|
|
nums[j] = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哨兵划分 */
|
|
|
|
|
function partition(nums: number[], left: number, right: number): number {
|
|
|
|
|
// 以 nums[left] 作为基准数
|
|
|
|
|
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; // 从左向右找首个大于基准数的元素
|
|
|
|
|
}
|
|
|
|
|
// 元素交换
|
|
|
|
|
swap(nums, i, j); // 交换这两个元素
|
|
|
|
|
}
|
|
|
|
|
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
|
|
|
|
return i; // 返回基准数的索引
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
@ -246,7 +269,18 @@ comments: true
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
|
|
|
|
|
|
|
|
|
/* 快速排序 */
|
|
|
|
|
function quickSort(nums: number[], left: number, right: number): void {
|
|
|
|
|
// 子数组长度为 1 时终止递归
|
|
|
|
|
if (left >= right) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 哨兵划分
|
|
|
|
|
const pivot = partition(nums, left, right);
|
|
|
|
|
// 递归左子数组、右子数组
|
|
|
|
|
quickSort(nums, left, pivot - 1);
|
|
|
|
|
quickSort(nums, pivot + 1, right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
@ -401,7 +435,27 @@ comments: true
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
|
|
|
|
/* 选取三个元素的中位数 */
|
|
|
|
|
function medianThree(nums: number[], left: number, mid: number, right: number): number {
|
|
|
|
|
// 使用了异或操作来简化代码
|
|
|
|
|
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
|
|
|
|
if (Number(nums[left] > nums[mid]) ^ Number(nums[left] > nums[right])) {
|
|
|
|
|
return left;
|
|
|
|
|
} else if (Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])) {
|
|
|
|
|
return mid;
|
|
|
|
|
} else {
|
|
|
|
|
return right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哨兵划分(三数取中值) */
|
|
|
|
|
function partition(nums: number[], left: number, right: number): number {
|
|
|
|
|
// 选取三个候选元素的中位数
|
|
|
|
|
let med = medianThree(nums, left, Math.floor((left + right) / 2), right);
|
|
|
|
|
// 将中位数交换至数组最左端
|
|
|
|
|
swap(nums, left, med);
|
|
|
|
|
// 以 nums[left] 作为基准数
|
|
|
|
|
// 下同省略...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
@ -492,7 +546,7 @@ comments: true
|
|
|
|
|
|
|
|
|
|
```js title="quick_sort.js"
|
|
|
|
|
/* 快速排序(尾递归优化) */
|
|
|
|
|
quickSort(nums, left, right) {
|
|
|
|
|
function quickSort(nums, left, right) {
|
|
|
|
|
// 子数组长度为 1 时终止
|
|
|
|
|
while (left < right) {
|
|
|
|
|
// 哨兵划分操作
|
|
|
|
@ -512,7 +566,22 @@ comments: true
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="quick_sort.ts"
|
|
|
|
|
|
|
|
|
|
/* 快速排序(尾递归优化) */
|
|
|
|
|
function quickSort(nums: number[], left: number, right: number): void {
|
|
|
|
|
// 子数组长度为 1 时终止
|
|
|
|
|
while (left < right) {
|
|
|
|
|
// 哨兵划分操作
|
|
|
|
|
let pivot = partition(nums, left, right);
|
|
|
|
|
// 对两个子数组中较短的那个执行快排
|
|
|
|
|
if (pivot - left < right - pivot) {
|
|
|
|
|
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
|
|
|
|
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
|
|
|
|
} else {
|
|
|
|
|
quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
|
|
|
|
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|