Skip to content

Commit

Permalink
this is commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Abey Bernard committed Oct 22, 2023
1 parent a0a64fa commit 2ba7bd5
Showing 1 changed file with 8 additions and 41 deletions.
49 changes: 8 additions & 41 deletions src/sorting/quick_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
/// after `x. All this should be done in linear time.
///
/// QuickSort's time complexity is O(n*logn).
pub fn quick_sort<T: Ord>(array: &mut [T]) {
use crate::sorting::traits::Sorter;

fn quick_sort<T: Ord>(array: &mut [T]) {
match array.len() {
0 | 1 => return,
_ => {}
Expand All @@ -22,7 +24,6 @@ pub fn quick_sort<T: Ord>(array: &mut [T]) {
let (pivot, rest) = array.split_first_mut().expect("array is non-empty");
let mut left = 0;
let mut right = rest.len() - 1;

while left <= right {
if &rest[left] <= pivot {
left += 1;
Expand All @@ -46,12 +47,7 @@ pub fn quick_sort<T: Ord>(array: &mut [T]) {
let (left, right) = array.split_at_mut(left);
quick_sort(left);
quick_sort(&mut right[1..]);
}

/// QuickSort is a type that implements the `Sorter` trait for QuickSort.
pub struct QuickSort;

impl<T> Sorter<T> for QuickSort
}impl<T> Sorter<T> for QuickSort

Check failure on line 50 in src/sorting/quick_sort.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `QuickSort` in this scope

Check failure on line 50 in src/sorting/quick_sort.rs

View workflow job for this annotation

GitHub Actions / Check

cannot find type `QuickSort` in this scope
where
T: Ord + Copy,
{
Expand All @@ -60,40 +56,11 @@ where
}
}

// Example module organization structure
mod sorting {
pub mod traits {
pub trait Sorter<T> {
fn sort_inplace(array: &mut [T]);
}
}

pub mod quicksort {
use super::traits::Sorter;

/// Sorts an array using the QuickSort algorithm.
pub fn quick_sort<T: Ord>(array: &mut [T]) {
// ... (QuickSort implementation)
}

/// QuickSort is a type that implements the `Sorter` trait for QuickSort.
pub struct QuickSort;

impl<T> Sorter<T> for QuickSort
where
T: Ord + Copy,
{
fn sort_inplace(array: &mut [T]) {
quick_sort(array);
}
}
}
}

#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;

Check warning on line 61 in src/sorting/quick_sort.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `crate::sorting::traits::Sorter`
use crate::sorting::quicksort::QuickSort;
use crate::sorting::QuickSort;

// Add your unit tests here
}
sorting_tests!(QuickSort::sort, quick_sort);
sorting_tests!(QuickSort::sort_inplace, quick_sort, inplace);
}

0 comments on commit 2ba7bd5

Please sign in to comment.