Skip to content

Commit

Permalink
Refactor quicksort implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Nov 12, 2023
1 parent b84fbf6 commit cf20c53
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/quicksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,18 @@ where
// This is the recursive case where we plan
// to minimize the size of the problem thus
// applying a D&C mindset
let pivot = coll_clone.get(0).unwrap();
let mut less_than_pivot: Vec<T> = quicksort(
&coll_clone
.clone()
.into_iter()
.filter(|i| *i < *pivot)
.collect::<Vec<T>>(),
);
let greather_than_pivot: Vec<T> = quicksort(
&coll_clone
.clone()
.into_iter()
.filter(|i| *i > *pivot)
.collect::<Vec<T>>(),
);

less_than_pivot.push(*pivot);

less_than_pivot
.iter()
.chain(greather_than_pivot.iter())
.copied()
.collect()
let pivot = coll_clone.first().unwrap();

let less_than_pivot: Vec<T> = quicksort(&coll.iter().filter(|i| *i < pivot).cloned().collect());
let mut greater_than_pivot: Vec<T> =
quicksort(&coll.iter().filter(|i| *i > pivot).cloned().collect());

let mut result = less_than_pivot;

result.push(*pivot);
result.append(&mut greater_than_pivot);

result
}

#[cfg(test)]
Expand Down

0 comments on commit cf20c53

Please sign in to comment.