Skip to content

Commit

Permalink
this is commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Abey Bernard committed Oct 22, 2023
1 parent 1ca5f59 commit a0a64fa
Showing 1 changed file with 50 additions and 15 deletions.
65 changes: 50 additions & 15 deletions src/sorting/quick_sort.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
use crate::sorting::traits::Sorter;

fn quick_sort<T: Ord>(array: &mut [T]) {
/// Sorts an array using the QuickSort algorithm.
///
/// QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions
/// the given array around the picked pivot.
///
/// # Parameters
///
/// - `array`: A mutable reference to the array to be sorted.
///
/// The key process in QuickSort is a partition. The target of partitions is, given an array and an
/// element `x` of an array as the pivot, put `x` at its correct position in a sorted array and put
/// all smaller elements (smaller than `x`) before `x`, and put all greater elements (greater than `x`)
/// 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]) {
match array.len() {
0 | 1 => return,
_ => {}
Expand All @@ -9,6 +22,7 @@ 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 @@ -34,15 +48,7 @@ fn quick_sort<T: Ord>(array: &mut [T]) {
quick_sort(&mut right[1..]);
}

/// QuickSort is a Divide and Conquer algorithm. It picks an element as
/// a pivot and partitions the given array around the picked pivot.
/// There are many different versions of quickSort that pick pivot in different ways.
/// parameters takes an array
/// The key process in quickSort is a partition().
/// The target of partitions is, given an array and an element x of an array as the pivot,
/// put x at its correct position in a sorted array and put all smaller elements (smaller than x) before x,
/// and put all greater elements (greater than x) after x. All this should be done in linear time.
/// Quicksort's time complexity is O(n*logn) .
/// QuickSort is a type that implements the `Sorter` trait for QuickSort.
pub struct QuickSort;

impl<T> Sorter<T> for QuickSort

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

View workflow job for this annotation

GitHub Actions / Check

cannot find trait `Sorter` in this scope

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

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find trait `Sorter` in this scope
Expand All @@ -54,11 +60,40 @@ 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 95 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;
use crate::sorting::quicksort::QuickSort;

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

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved import `crate::sorting::quicksort`

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

0 comments on commit a0a64fa

Please sign in to comment.