From 5f0ae848c44002309ef820db3ed4b5aa57f57405 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Sun, 15 Jan 2023 23:32:58 +0800 Subject: [PATCH] Update medianThree() in quick_sort. --- codes/cpp/chapter_sorting/quick_sort.cpp | 2 +- codes/csharp/chapter_sorting/quick_sort.cs | 2 +- codes/go/chapter_sorting/quick_sort.go | 6 +++-- codes/java/chapter_sorting/quick_sort.java | 2 +- .../javascript/chapter_sorting/quick_sort.js | 2 +- codes/python/chapter_sorting/quick_sort.py | 2 +- .../typescript/chapter_sorting/quick_sort.ts | 2 +- docs/chapter_sorting/quick_sort.md | 23 ++++++++++++------- 8 files changed, 25 insertions(+), 16 deletions(-) diff --git a/codes/cpp/chapter_sorting/quick_sort.cpp b/codes/cpp/chapter_sorting/quick_sort.cpp index c298f5be8..e72691bc6 100644 --- a/codes/cpp/chapter_sorting/quick_sort.cpp +++ b/codes/cpp/chapter_sorting/quick_sort.cpp @@ -59,7 +59,7 @@ private: 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])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; diff --git a/codes/csharp/chapter_sorting/quick_sort.cs b/codes/csharp/chapter_sorting/quick_sort.cs index 020d4f342..90de5c192 100644 --- a/codes/csharp/chapter_sorting/quick_sort.cs +++ b/codes/csharp/chapter_sorting/quick_sort.cs @@ -65,7 +65,7 @@ namespace hello_algo.chapter_sorting { // 使用了异或操作来简化代码 // 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; diff --git a/codes/go/chapter_sorting/quick_sort.go b/codes/go/chapter_sorting/quick_sort.go index 853bce452..ab7a2575c 100644 --- a/codes/go/chapter_sorting/quick_sort.go +++ b/codes/go/chapter_sorting/quick_sort.go @@ -47,9 +47,11 @@ func (q *quickSort) quickSort(nums []int, left, right int) { /* 选取三个元素的中位数 */ func (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int { - if (nums[left] > nums[mid]) != (nums[left] > nums[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]) { + } else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) { return mid } return right diff --git a/codes/java/chapter_sorting/quick_sort.java b/codes/java/chapter_sorting/quick_sort.java index b121b757b..1eaa1011d 100644 --- a/codes/java/chapter_sorting/quick_sort.java +++ b/codes/java/chapter_sorting/quick_sort.java @@ -58,7 +58,7 @@ 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])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; diff --git a/codes/javascript/chapter_sorting/quick_sort.js b/codes/javascript/chapter_sorting/quick_sort.js index 47ac100fd..3915a178f 100644 --- a/codes/javascript/chapter_sorting/quick_sort.js +++ b/codes/javascript/chapter_sorting/quick_sort.js @@ -56,7 +56,7 @@ 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; + 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; } diff --git a/codes/python/chapter_sorting/quick_sort.py b/codes/python/chapter_sorting/quick_sort.py index f5ce8120c..75245b617 100644 --- a/codes/python/chapter_sorting/quick_sort.py +++ b/codes/python/chapter_sorting/quick_sort.py @@ -42,7 +42,7 @@ class QuickSortMedian: def median_three(self, nums, left, mid, right): # 使用了异或操作来简化代码 # 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if (nums[left] > nums[mid]) ^ (nums[left] > nums[right]): + if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]): return left elif (nums[mid] < nums[left]) ^ (nums[mid] > nums[right]): return mid diff --git a/codes/typescript/chapter_sorting/quick_sort.ts b/codes/typescript/chapter_sorting/quick_sort.ts index 6379e08fc..1f6de5a32 100644 --- a/codes/typescript/chapter_sorting/quick_sort.ts +++ b/codes/typescript/chapter_sorting/quick_sort.ts @@ -58,7 +58,7 @@ class QuickSortMedian { 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])) { + 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; diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index 62ab2147f..06f04efd5 100644 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -16,20 +16,28 @@ comments: true === "Step 1" ![pivot_division_step1](quick_sort.assets/pivot_division_step1.png) + === "Step 2" ![pivot_division_step2](quick_sort.assets/pivot_division_step2.png) + === "Step 3" ![pivot_division_step3](quick_sort.assets/pivot_division_step3.png) + === "Step 4" ![pivot_division_step4](quick_sort.assets/pivot_division_step4.png) + === "Step 5" ![pivot_division_step5](quick_sort.assets/pivot_division_step5.png) + === "Step 6" ![pivot_division_step6](quick_sort.assets/pivot_division_step6.png) + === "Step 7" ![pivot_division_step7](quick_sort.assets/pivot_division_step7.png) + === "Step 8" ![pivot_division_step8](quick_sort.assets/pivot_division_step8.png) + === "Step 9" ![pivot_division_step9](quick_sort.assets/pivot_division_step9.png) @@ -220,7 +228,6 @@ comments: true swap(nums, i, left); // 将基准数交换至两子数组的分界线 return i; // 返回基准数的索引 } - ``` === "Swift" @@ -408,7 +415,7 @@ comments: true 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])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; @@ -434,7 +441,7 @@ comments: true 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])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; @@ -460,7 +467,7 @@ comments: true def median_three(self, nums, left, mid, right): # 使用了异或操作来简化代码 # 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if (nums[left] > nums[mid]) ^ (nums[left] > nums[right]): + if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]): return left elif (nums[mid] < nums[left]) ^ (nums[mid] > nums[right]): return mid @@ -481,7 +488,7 @@ comments: true ```go title="quick_sort.go" /* 选取三个元素的中位数 */ func medianThree(nums []int, left, mid, right int) int { - if (nums[left] > nums[mid]) != (nums[left] > nums[right]) { + if (nums[left] < nums[mid]) != (nums[left] < nums[right]) { return left } else if (nums[mid] < nums[left]) != (nums[mid] > nums[right]) { return mid @@ -507,7 +514,7 @@ comments: true function medianThree(nums, left, mid, right) { // 使用了异或操作来简化代码 // 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; @@ -533,7 +540,7 @@ comments: true 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])) { + 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; @@ -566,7 +573,7 @@ comments: true { // 使用了异或操作来简化代码 // 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 - if ((nums[left] > nums[mid]) ^ (nums[left] > nums[right])) + if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; else if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid;