Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: organize QuickSort comments #64

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
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).
Copy link
Owner

Choose a reason for hiding this comment

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

I like this change, it is an improvement 👍🏻

alexfertel marked this conversation as resolved.
Show resolved Hide resolved
pub fn quick_sort<T: Ord>(array: &mut [T]) {
match array.len() {
0 | 1 => return,
_ => {}
Expand All @@ -9,6 +22,7 @@
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,18 +48,10 @@
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
where
T: Ord + Copy,
{
Expand All @@ -54,11 +60,40 @@
}
}

// 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);
}
}
}
}

Copy link
Owner

Choose a reason for hiding this comment

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

Could we remove this bit?

#[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);
Copy link
Owner

Choose a reason for hiding this comment

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

Could we revert these changes?

// Add your unit tests here
}
Loading