Skip to content

Commit

Permalink
Set correct starting position for high cursor (TheAlgorithms#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrellpf authored Jan 5, 2024
1 parent 8894616 commit f850766
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/sorting/quick_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ use std::cmp::PartialOrd;
pub fn partition<T: PartialOrd>(arr: &mut [T], lo: usize, hi: usize) -> usize {
let pivot = hi;
let mut i = lo;
let mut j = hi;
let mut j = hi - 1;

loop {
while arr[i] < arr[pivot] {
i += 1;
}
while j > 0 && arr[j - 1] > arr[pivot] {
while j > 0 && arr[j] > arr[pivot] {
j -= 1;
}
if j == 0 || i >= j - 1 {
if j == 0 || i >= j {
break;
} else if arr[i] == arr[j - 1] {
} else if arr[i] == arr[j] {
i += 1;
j -= 1;
} else {
arr.swap(i, j - 1);
arr.swap(i, j);
}
}
arr.swap(i, pivot);
Expand Down

0 comments on commit f850766

Please sign in to comment.