Skip to content

Commit

Permalink
Update quick_sort.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets authored Mar 14, 2024
1 parent 30d7e63 commit f3e3195
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions codes/rust/chapter_sorting/quick_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit f3e3195

Please sign in to comment.