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
pull/1140/head
Yudong Jin 8 months ago committed by GitHub
parent 644eaf99ff
commit 9769e14017
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -52,13 +52,11 @@ 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
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;
}

@ -57,13 +57,11 @@ class QuickSortMedian {
/* 选取三个候选元素的中位数 */
static int medianThree(vector<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
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;
}

@ -49,13 +49,11 @@ 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
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;
}

@ -49,13 +49,11 @@ class QuickSortMedian {
/* 选取三个候选元素的中位数 */
static int _medianThree(List<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
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;
}

@ -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
}

@ -56,13 +56,11 @@ 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
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;
}

@ -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;
}
/* 哨兵划分(三数取中值) */

@ -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:

@ -46,12 +46,12 @@ 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
}

@ -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;
}
/* */

@ -62,18 +62,15 @@ 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 {
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;
}
}
/* 哨兵划分(三数取中值) */
partition(nums: number[], left: number, right: number): number {

@ -53,16 +53,15 @@ 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 {
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;
}
}
//
pub fn partition(nums: []i32, left: usize, right: usize) usize {

Loading…
Cancel
Save