-
Notifications
You must be signed in to change notification settings - Fork 71
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
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
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, | ||
_ => {} | ||
|
@@ -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; | ||
|
@@ -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 GitHub Actions / Check
|
||
where | ||
T: Ord + Copy, | ||
{ | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
use crate::sorting::QuickSort; | ||
use crate::sorting::quicksort::QuickSort; | ||
|
||
sorting_tests!(QuickSort::sort, quick_sort); | ||
sorting_tests!(QuickSort::sort_inplace, quick_sort, inplace); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we revert these changes? |
||
// Add your unit tests here | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 👍🏻