Update medianThree() in quick_sort.

This commit is contained in:
Yudong Jin
2023-01-15 23:32:58 +08:00
parent 3e19205c84
commit 5f0ae848c4
8 changed files with 25 additions and 16 deletions

View File

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