From f3e319522dd2dd934f55b11564061a2da8e30a36 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 14 Mar 2024 20:10:47 +0800 Subject: [PATCH] Update quick_sort.rs --- codes/rust/chapter_sorting/quick_sort.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/codes/rust/chapter_sorting/quick_sort.rs b/codes/rust/chapter_sorting/quick_sort.rs index 77dcf1c9a8..cd44248e20 100644 --- a/codes/rust/chapter_sorting/quick_sort.rs +++ b/codes/rust/chapter_sorting/quick_sort.rs @@ -46,11 +46,13 @@ struct QuickSortMedian; impl QuickSortMedian { /* 选取三个候选元素的中位数 */ fn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize { - let 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 之间 + 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 }