Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions src/quicksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,32 @@ fn quicksort<T: Ord>(slice: &mut [T]) {
let (pivot, rest) = slice.split_first_mut().expect("slice is non-empty");
let mut left = 0;
let mut right = rest.len() - 1;
while left <= right {
// if right <= 0, it's done
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also point out here (this helped me) that if right <= 0, then left == right, so there can't be a need to swap.

while left <= right && right > 0 {
if &rest[left] <= pivot {
// already on the correct side
left += 1;
} else if &rest[right] > pivot {
} else if &rest[right] > pivot && right > 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this conditional change is necessary? After all, we won't get here with the new right > 0 in the while conditional.

// right already on the correct side
// avoid unnecessary swaps back and forth
if right == 0 {
// we must be done
break;
}
right -= 1;
} else {
// left holds a right, and right holds a left, swap them.
rest.swap(left, right);
left += 1;
if right == 0 {
// we must be done
break;
}
right -= 1;
}
}

// re-align left to account for the pivot at 0
let left = left + 1;

// place the pivot at its final location
slice.swap(0, left - 1);

// split_at_mut(mid: usize) -> (&mut [..mid), &mut [mid..])
let (left, right) = slice.split_at_mut(left - 1);
assert!(left.last() <= right.first());
quicksort(left);
quicksort(&mut right[1..]);
slice.swap(0, left);

// left is the pivot index
// less_pivot = [0, left) greater_pivot = (left, slice.len() - 1]
let (less_pivot, greater_pivot) = slice.split_at_mut(left);
assert!(less_pivot.last() <= greater_pivot.first());
quicksort(less_pivot);
quicksort(&mut greater_pivot[1..]);
}

impl<T> Sorter<T> for QuickSort {
Expand Down