Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the median_three mehod for quick sort #1134

Merged
merged 8 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions codes/c/chapter_sorting/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 哨兵划分(三数取中值) */
Expand Down
14 changes: 6 additions & 8 deletions codes/cpp/chapter_sorting/quick_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ 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
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;
}

/* 哨兵划分(三数取中值) */
Expand Down
14 changes: 6 additions & 8 deletions codes/csharp/chapter_sorting/quick_sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ static void Swap(int[] nums, int i, int j) {

/* 选取三个候选元素的中位数 */
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;
}

/* 哨兵划分(三数取中值) */
Expand Down
14 changes: 6 additions & 8 deletions codes/dart/chapter_sorting/quick_sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ 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
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;
}

/* 哨兵划分(三数取中值) */
Expand Down
12 changes: 6 additions & 6 deletions codes/go/chapter_sorting/quick_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 6 additions & 8 deletions codes/java/chapter_sorting/quick_sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ static void swap(int[] nums, int i, int j) {

/* 选取三个候选元素的中位数 */
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;
}

/* 哨兵划分(三数取中值) */
Expand Down
14 changes: 8 additions & 6 deletions codes/javascript/chapter_sorting/quick_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 哨兵划分(三数取中值) */
Expand Down
11 changes: 5 additions & 6 deletions codes/python/chapter_sorting/quick_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions codes/rust/chapter_sorting/quick_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -73,8 +73,8 @@ impl QuickSortMedian {
}
nums.swap(i, j); // 交换这两个元素
}
nums.swap(i, left); // 将基准数交换至两子数组的分界线
i // 返回基准数的索引
nums.swap(i, left); // 将基准数交换至两子数组的分界线
i // 返回基准数的索引
}

/* 快速排序 */
Expand Down
15 changes: 9 additions & 6 deletions codes/swift/chapter_sorting/quick_sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 哨兵划分(三数取中值) */
Expand Down
19 changes: 8 additions & 11 deletions codes/typescript/chapter_sorting/quick_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/* 哨兵划分(三数取中值) */
Expand Down
17 changes: 8 additions & 9 deletions codes/zig/chapter_sorting/quick_sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// 哨兵划分(三数取中值)
Expand Down
Loading