mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
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
This commit is contained in:
parent
644eaf99ff
commit
9769e14017
12 changed files with 83 additions and 93 deletions
|
@ -52,13 +52,11 @@ void quickSort(int nums[], int left, int right) {
|
||||||
/* 快速排序类(中位基准数优化) */
|
/* 快速排序类(中位基准数优化) */
|
||||||
// 选取三个候选元素的中位数
|
// 选取三个候选元素的中位数
|
||||||
int medianThree(int nums[], int left, int mid, int right) {
|
int medianThree(int nums[], int left, int mid, int right) {
|
||||||
// 此处使用异或运算来简化代码
|
int l = nums[left], m = nums[mid], r = nums[right];
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
return mid; // m 在 l 和 r 之间
|
||||||
return left;
|
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
return left; // l 在 m 和 r 之间
|
||||||
return mid;
|
|
||||||
else
|
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,13 +57,11 @@ class QuickSortMedian {
|
||||||
|
|
||||||
/* 选取三个候选元素的中位数 */
|
/* 选取三个候选元素的中位数 */
|
||||||
static int medianThree(vector<int> &nums, int left, int mid, int right) {
|
static int medianThree(vector<int> &nums, int left, int mid, int right) {
|
||||||
// 此处使用异或运算来简化代码
|
int l = nums[left], m = nums[mid], r = nums[right];
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
return mid; // m 在 l 和 r 之间
|
||||||
return left;
|
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
return left; // l 在 m 和 r 之间
|
||||||
return mid;
|
|
||||||
else
|
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,13 +49,11 @@ class QuickSortMedian {
|
||||||
|
|
||||||
/* 选取三个候选元素的中位数 */
|
/* 选取三个候选元素的中位数 */
|
||||||
static int MedianThree(int[] nums, int left, int mid, int right) {
|
static int MedianThree(int[] nums, int left, int mid, int right) {
|
||||||
// 此处使用异或运算来简化代码
|
int l = nums[left], m = nums[mid], r = nums[right];
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
return mid; // m 在 l 和 r 之间
|
||||||
return left;
|
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
return left; // l 在 m 和 r 之间
|
||||||
return mid;
|
|
||||||
else
|
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,13 +49,11 @@ class QuickSortMedian {
|
||||||
|
|
||||||
/* 选取三个候选元素的中位数 */
|
/* 选取三个候选元素的中位数 */
|
||||||
static int _medianThree(List<int> nums, int left, int mid, int right) {
|
static int _medianThree(List<int> nums, int left, int mid, int right) {
|
||||||
// 此处使用异或运算来简化代码
|
int l = nums[left], m = nums[mid], r = nums[right];
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
return mid; // m 在 l 和 r 之间
|
||||||
return left;
|
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
return left; // l 在 m 和 r 之间
|
||||||
return mid;
|
|
||||||
else
|
|
||||||
return right;
|
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 {
|
func (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {
|
||||||
// 此处使用异或运算来简化代码(!= 在这里起到异或的作用)
|
l, m, r := nums[left], nums[mid], nums[right]
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if (l <= m && m <= r) || (r <= m && m <= l) {
|
||||||
if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {
|
return mid // m 在 l 和 r 之间
|
||||||
return left
|
}
|
||||||
} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {
|
if (m <= l && l <= r) || (r <= l && l <= m) {
|
||||||
return mid
|
return left // l 在 m 和 r 之间
|
||||||
}
|
}
|
||||||
return right
|
return right
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,13 +56,11 @@ class QuickSortMedian {
|
||||||
|
|
||||||
/* 选取三个候选元素的中位数 */
|
/* 选取三个候选元素的中位数 */
|
||||||
static int medianThree(int[] nums, int left, int mid, int right) {
|
static int medianThree(int[] nums, int left, int mid, int right) {
|
||||||
// 此处使用异或运算来简化代码
|
int l = nums[left], m = nums[mid], r = nums[right];
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))
|
return mid; // m 在 l 和 r 之间
|
||||||
return left;
|
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
return left; // l 在 m 和 r 之间
|
||||||
return mid;
|
|
||||||
else
|
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,12 +55,14 @@ class QuickSortMedian {
|
||||||
|
|
||||||
/* 选取三个候选元素的中位数 */
|
/* 选取三个候选元素的中位数 */
|
||||||
medianThree(nums, left, mid, right) {
|
medianThree(nums, left, mid, right) {
|
||||||
// 此处使用异或运算来简化代码
|
let l = nums[left],
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
m = nums[mid],
|
||||||
if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;
|
r = nums[right];
|
||||||
else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))
|
// m 在 l 和 r 之间
|
||||||
return mid;
|
if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;
|
||||||
else return right;
|
// 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:
|
def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:
|
||||||
"""选取三个候选元素的中位数"""
|
"""选取三个候选元素的中位数"""
|
||||||
# 此处使用异或运算来简化代码
|
l, m, r = nums[left], nums[mid], nums[right]
|
||||||
# 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if (l <= m <= r) or (r <= m <= l):
|
||||||
if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):
|
return mid # m 在 l 和 r 之间
|
||||||
return left
|
if (m <= l <= r) or (r <= l <= m):
|
||||||
elif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]):
|
return left # l 在 m 和 r 之间
|
||||||
return mid
|
|
||||||
return right
|
return right
|
||||||
|
|
||||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||||
|
|
|
@ -46,12 +46,12 @@ struct QuickSortMedian;
|
||||||
impl QuickSortMedian {
|
impl QuickSortMedian {
|
||||||
/* 选取三个候选元素的中位数 */
|
/* 选取三个候选元素的中位数 */
|
||||||
fn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {
|
fn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {
|
||||||
// 此处使用异或运算来简化代码
|
let (mut l, mut m, mut r) = (nums[left], nums[mid], nums[right]);
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
if ((l <= m && m <= r) || (r <= m && m <= l)) {
|
||||||
if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]) {
|
return mid; // m 在 l 和 r 之间
|
||||||
return left;
|
}
|
||||||
} else if (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]) {
|
if ((m <= l && l <= r) || (r <= l && l <= m)) {
|
||||||
return mid;
|
return left; // l 在 m 和 r 之间
|
||||||
}
|
}
|
||||||
right
|
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 {
|
func medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {
|
||||||
if (nums[left] < nums[mid]) != (nums[left] < nums[right]) {
|
let l = nums[left]
|
||||||
return left
|
let m = nums[mid]
|
||||||
} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {
|
let r = nums[right]
|
||||||
return mid
|
if (l <= m && m <= r) || (r <= m && m <= l) {
|
||||||
} else {
|
return mid; // m 在 l 和 r 之间
|
||||||
return right
|
|
||||||
}
|
}
|
||||||
|
if (m <= l && l <= r) || (r <= l && l <= m) {
|
||||||
|
return left; // l 在 m 和 r 之间
|
||||||
|
}
|
||||||
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 哨兵划分(三数取中值) */
|
/* 哨兵划分(三数取中值) */
|
||||||
|
|
|
@ -62,18 +62,15 @@ class QuickSortMedian {
|
||||||
mid: number,
|
mid: number,
|
||||||
right: number
|
right: number
|
||||||
): number {
|
): number {
|
||||||
// 此处使用异或运算来简化代码
|
let l = nums[left],
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
m = nums[mid],
|
||||||
if (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {
|
r = nums[right];
|
||||||
return left;
|
// m 在 l 和 r 之间
|
||||||
} else if (
|
if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;
|
||||||
Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])
|
// l 在 m 和 r 之间
|
||||||
) {
|
if ((m <= l && l <= r) || (r <= l && l <= m)) return left;
|
||||||
return mid;
|
|
||||||
} else {
|
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* 哨兵划分(三数取中值) */
|
/* 哨兵划分(三数取中值) */
|
||||||
partition(nums: number[], left: number, right: number): number {
|
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 {
|
pub fn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {
|
||||||
// 此处使用异或运算来简化代码
|
var l = nums[left];
|
||||||
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
var m = nums[mid];
|
||||||
if ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {
|
var r = nums[right];
|
||||||
return left;
|
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||||
} else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {
|
return mid; // m 在 l 和 r 之间
|
||||||
return mid;
|
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||||
} else {
|
return left; // l 在 m 和 r 之间
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 哨兵划分(三数取中值)
|
// 哨兵划分(三数取中值)
|
||||||
pub fn partition(nums: []i32, left: usize, right: usize) usize {
|
pub fn partition(nums: []i32, left: usize, right: usize) usize {
|
||||||
|
|
Loading…
Reference in a new issue