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 all commits
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
46 changes: 16 additions & 30 deletions src/sorting/quick_sort.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
//! 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).
use crate::sorting::traits::Sorter;

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

View workflow job for this annotation

GitHub Actions / Check

unused import: `crate::sorting::traits::Sorter`

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

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `crate::sorting::traits::Sorter`

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

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `crate::sorting::traits::Sorter`

fn quick_sort<T: Ord>(array: &mut [T]) {
match array.len() {
Expand Down Expand Up @@ -32,33 +47,4 @@
let (left, right) = array.split_at_mut(left);
quick_sort(left);
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) .
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;
use crate::sorting::QuickSort;

sorting_tests!(QuickSort::sort, quick_sort);
sorting_tests!(QuickSort::sort_inplace, quick_sort, inplace);
}
Comment on lines -57 to -64
Copy link
Owner

Choose a reason for hiding this comment

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

Please add the tests back.

}
Loading