From 9769e140170da6652331a2fc514573d035c9cab4 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 15 Mar 2024 01:06:59 +0800 Subject: [PATCH] Fix the median_three mehod for quick sort (#1134) * Add the section of terminologies. * fix format * Fix median_three function for quick sort * Delete docs/chapter_appendix/terminologies.md * Update quick_sort.ts * Update quick_sort.rs * Update quick_sort.js --- codes/c/chapter_sorting/quick_sort.c | 14 ++++++-------- codes/cpp/chapter_sorting/quick_sort.cpp | 14 ++++++-------- codes/csharp/chapter_sorting/quick_sort.cs | 14 ++++++-------- codes/dart/chapter_sorting/quick_sort.dart | 14 ++++++-------- codes/go/chapter_sorting/quick_sort.go | 12 ++++++------ codes/java/chapter_sorting/quick_sort.java | 14 ++++++-------- .../javascript/chapter_sorting/quick_sort.js | 14 ++++++++------ codes/python/chapter_sorting/quick_sort.py | 11 +++++------ codes/rust/chapter_sorting/quick_sort.rs | 18 +++++++++--------- codes/swift/chapter_sorting/quick_sort.swift | 15 +++++++++------ .../typescript/chapter_sorting/quick_sort.ts | 19 ++++++++----------- codes/zig/chapter_sorting/quick_sort.zig | 17 ++++++++--------- 12 files changed, 83 insertions(+), 93 deletions(-) diff --git a/codes/c/chapter_sorting/quick_sort.c b/codes/c/chapter_sorting/quick_sort.c index 81202c7a0..50a95705d 100644 --- a/codes/c/chapter_sorting/quick_sort.c +++ b/codes/c/chapter_sorting/quick_sort.c @@ -52,14 +52,12 @@ void quickSort(int nums[], int left, int right) { /* 快速排序类(中位基准数优化) */ // 选取三个候选元素的中位数 int medianThree(int nums[], int left, int mid, int 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; + int l = nums[left], m = nums[mid], r = nums[right]; + if ((l <= m && m <= r) || (r <= m && m <= l)) + return mid; // m 在 l 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) + return left; // l 在 m 和 r 之间 + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/cpp/chapter_sorting/quick_sort.cpp b/codes/cpp/chapter_sorting/quick_sort.cpp index 32d3fb4bc..e0b48ac24 100644 --- a/codes/cpp/chapter_sorting/quick_sort.cpp +++ b/codes/cpp/chapter_sorting/quick_sort.cpp @@ -57,14 +57,12 @@ class QuickSortMedian { /* 选取三个候选元素的中位数 */ static int medianThree(vector &nums, int left, int mid, int 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; + int l = nums[left], m = nums[mid], r = nums[right]; + if ((l <= m && m <= r) || (r <= m && m <= l)) + return mid; // m 在 l 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) + return left; // l 在 m 和 r 之间 + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/csharp/chapter_sorting/quick_sort.cs b/codes/csharp/chapter_sorting/quick_sort.cs index b9badb9f4..055c553b7 100644 --- a/codes/csharp/chapter_sorting/quick_sort.cs +++ b/codes/csharp/chapter_sorting/quick_sort.cs @@ -49,14 +49,12 @@ class QuickSortMedian { /* 选取三个候选元素的中位数 */ static int MedianThree(int[] nums, int left, int mid, int 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; + int l = nums[left], m = nums[mid], r = nums[right]; + if ((l <= m && m <= r) || (r <= m && m <= l)) + return mid; // m 在 l 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) + return left; // l 在 m 和 r 之间 + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/dart/chapter_sorting/quick_sort.dart b/codes/dart/chapter_sorting/quick_sort.dart index a3b49727d..5d635bf8a 100644 --- a/codes/dart/chapter_sorting/quick_sort.dart +++ b/codes/dart/chapter_sorting/quick_sort.dart @@ -49,14 +49,12 @@ class QuickSortMedian { /* 选取三个候选元素的中位数 */ static int _medianThree(List nums, int left, int mid, int 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; + int l = nums[left], m = nums[mid], r = nums[right]; + if ((l <= m && m <= r) || (r <= m && m <= l)) + return mid; // m 在 l 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) + return left; // l 在 m 和 r 之间 + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/go/chapter_sorting/quick_sort.go b/codes/go/chapter_sorting/quick_sort.go index c5eb95d3c..f5cbf1524 100644 --- a/codes/go/chapter_sorting/quick_sort.go +++ b/codes/go/chapter_sorting/quick_sort.go @@ -47,12 +47,12 @@ func (q *quickSort) quickSort(nums []int, left, right int) { /* 选取三个候选元素的中位数 */ func (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int { - // 此处使用异或运算来简化代码(!= 在这里起到异或的作用) - // 异或规则为 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 + l, m, r := nums[left], nums[mid], nums[right] + if (l <= m && m <= r) || (r <= m && m <= l) { + return mid // m 在 l 和 r 之间 + } + if (m <= l && l <= r) || (r <= l && l <= m) { + return left // l 在 m 和 r 之间 } return right } diff --git a/codes/java/chapter_sorting/quick_sort.java b/codes/java/chapter_sorting/quick_sort.java index 81125d571..a6c39c346 100644 --- a/codes/java/chapter_sorting/quick_sort.java +++ b/codes/java/chapter_sorting/quick_sort.java @@ -56,14 +56,12 @@ class QuickSortMedian { /* 选取三个候选元素的中位数 */ static int medianThree(int[] nums, int left, int mid, int 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; + int l = nums[left], m = nums[mid], r = nums[right]; + if ((l <= m && m <= r) || (r <= m && m <= l)) + return mid; // m 在 l 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) + return left; // l 在 m 和 r 之间 + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/javascript/chapter_sorting/quick_sort.js b/codes/javascript/chapter_sorting/quick_sort.js index 8e1622053..fd96b2d16 100644 --- a/codes/javascript/chapter_sorting/quick_sort.js +++ b/codes/javascript/chapter_sorting/quick_sort.js @@ -55,12 +55,14 @@ class QuickSortMedian { /* 选取三个候选元素的中位数 */ 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; + let l = nums[left], + m = nums[mid], + r = nums[right]; + // m 在 l 和 r 之间 + if ((l <= m && m <= r) || (r <= m && m <= l)) return mid; + // l 在 m 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) return left; + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/python/chapter_sorting/quick_sort.py b/codes/python/chapter_sorting/quick_sort.py index e2c73dc76..2c73cc1c5 100644 --- a/codes/python/chapter_sorting/quick_sort.py +++ b/codes/python/chapter_sorting/quick_sort.py @@ -40,12 +40,11 @@ class QuickSortMedian: def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int: """选取三个候选元素的中位数""" - # 此处使用异或运算来简化代码 - # 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]): - return left - elif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]): - return mid + l, m, r = nums[left], nums[mid], nums[right] + if (l <= m <= r) or (r <= m <= l): + return mid # m 在 l 和 r 之间 + if (m <= l <= r) or (r <= l <= m): + return left # l 在 m 和 r 之间 return right def partition(self, nums: list[int], left: int, right: int) -> int: diff --git a/codes/rust/chapter_sorting/quick_sort.rs b/codes/rust/chapter_sorting/quick_sort.rs index 509812ea0..cd44248e2 100644 --- a/codes/rust/chapter_sorting/quick_sort.rs +++ b/codes/rust/chapter_sorting/quick_sort.rs @@ -46,13 +46,13 @@ struct QuickSortMedian; impl QuickSortMedian { /* 选取三个候选元素的中位数 */ fn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize { - // 此处使用异或运算来简化代码 - // 异或规则为 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; - } + let (mut l, mut m, mut r) = (nums[left], nums[mid], nums[right]); + if ((l <= m && m <= r) || (r <= m && m <= l)) { + return mid; // m 在 l 和 r 之间 + } + if ((m <= l && l <= r) || (r <= l && l <= m)) { + return left; // l 在 m 和 r 之间 + } right } @@ -73,8 +73,8 @@ impl QuickSortMedian { } nums.swap(i, j); // 交换这两个元素 } - nums.swap(i, left); // 将基准数交换至两子数组的分界线 - i // 返回基准数的索引 + nums.swap(i, left); // 将基准数交换至两子数组的分界线 + i // 返回基准数的索引 } /* 快速排序 */ diff --git a/codes/swift/chapter_sorting/quick_sort.swift b/codes/swift/chapter_sorting/quick_sort.swift index 13b0a8f2a..21a72534b 100644 --- a/codes/swift/chapter_sorting/quick_sort.swift +++ b/codes/swift/chapter_sorting/quick_sort.swift @@ -47,13 +47,16 @@ func quickSort(nums: inout [Int], left: Int, right: Int) { /* 快速排序类(中位基准数优化) */ /* 选取三个候选元素的中位数 */ func medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int { - 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 + let l = nums[left] + let m = nums[mid] + let r = nums[right] + if (l <= m && m <= r) || (r <= m && m <= l) { + return mid; // m 在 l 和 r 之间 } + if (m <= l && l <= r) || (r <= l && l <= m) { + return left; // l 在 m 和 r 之间 + } + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/typescript/chapter_sorting/quick_sort.ts b/codes/typescript/chapter_sorting/quick_sort.ts index 5f4e7378c..f3ae3d8f3 100644 --- a/codes/typescript/chapter_sorting/quick_sort.ts +++ b/codes/typescript/chapter_sorting/quick_sort.ts @@ -62,17 +62,14 @@ class QuickSortMedian { 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; - } + let l = nums[left], + m = nums[mid], + r = nums[right]; + // m 在 l 和 r 之间 + if ((l <= m && m <= r) || (r <= m && m <= l)) return mid; + // l 在 m 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) return left; + return right; } /* 哨兵划分(三数取中值) */ diff --git a/codes/zig/chapter_sorting/quick_sort.zig b/codes/zig/chapter_sorting/quick_sort.zig index 9ad65f512..7f0cf5923 100644 --- a/codes/zig/chapter_sorting/quick_sort.zig +++ b/codes/zig/chapter_sorting/quick_sort.zig @@ -53,15 +53,14 @@ const QuickSortMedian = struct { // 选取三个候选元素的中位数 pub fn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize { - // 此处使用异或运算来简化代码 - // 异或规则为 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; - } + var l = nums[left]; + var m = nums[mid]; + var r = nums[right]; + if ((l <= m && m <= r) || (r <= m && m <= l)) + return mid; // m 在 l 和 r 之间 + if ((m <= l && l <= r) || (r <= l && l <= m)) + return left; // l 在 m 和 r 之间 + return right; } // 哨兵划分(三数取中值)