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

Strand sort #666

Closed
wants to merge 6 commits into from
Closed

Conversation

Github-Vektor
Copy link

Description

Strand Sort is a sorting algorithm that repeatedly takes elements from an unsorted list and adds them to a sorted list in a way that maintains the sorted order. It works by repeatedly finding the smallest element in the unsorted list and moving it to the sorted list until all elements are sorted. While it can be effective for small lists, it is not very efficient for larger datasets compared to other sorting algorithms like quicksort or mergesort.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

@codecov-commenter
Copy link

codecov-commenter commented Jan 21, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (338c386) 94.66% compared to head (6dfa6bf) 94.69%.
Report is 1 commits behind head on master.

Files Patch % Lines
src/sorting/strand_sort.rs 96.05% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #666      +/-   ##
==========================================
+ Coverage   94.66%   94.69%   +0.02%     
==========================================
  Files         290      293       +3     
  Lines       23161    23334     +173     
==========================================
+ Hits        21925    22095     +170     
- Misses       1236     1239       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vil02 vil02 mentioned this pull request Jan 21, 2024
10 tasks
Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

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

strand_sort modifies both of the inputs: ip and op. I do not see a reason for that. I would suggest either pub fn strand_sort(list: &mut LinkedList<i32>) or pub fn strand_sort(list: &LinkedList<i32>) -> LinkedList<i32> (with preference of the second one).

…d conformed file changes to pr review recommendation
return LinkedList::new();
}

let mut ip = list.clone();
Copy link
Member

Choose a reason for hiding this comment

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

Is this clone really needed? Could we just have some index of active element in the (input) list?

while !op.is_empty() || !sublist.is_empty() {
match (op.front(), sublist.front()) {
(Some(&op_val), Some(&sub_val)) if op_val <= sub_val => {
merged.push_back(op.pop_front().unwrap());
Copy link
Member

Choose a reason for hiding this comment

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

This is not covered by any test.

@@ -203,7 +202,20 @@ This card game is turned into a two-phase sorting algorithm, as follows. Given a

__Properties__
* Worst case performance O(n log n)
* Best case performance O(n)
* Best case performance O(n)\
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Best case performance O(n)\
* Best case performance O(n)


### [Strand Sort](./strand_sort.rs)

Strand Sort is a sorting algorithm that works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with the already sorted part. It is particularly effective for sorting lists where there are large numbers of ordered elements. The algorithm is intuitive and simple, iterating through the list, picking up elements in order, and merging these 'strands' into a final sorted list.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Strand Sort is a sorting algorithm that works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with the already sorted part. It is particularly effective for sorting lists where there are large numbers of ordered elements. The algorithm is intuitive and simple, iterating through the list, picking up elements in order, and merging these 'strands' into a final sorted list.
Strand Sort is a sorting algorithm that works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with the already sorted part. It is particularly effective for sorting lists where there are large numbers of ordered elements. The algorithm is intuitive and simple, iterating through the list, picking up elements in order, and merging these _strands_ into a final sorted list.

Comment on lines +12 to +33
let mut sublist = LinkedList::new();
sublist.push_back(ip.pop_front().unwrap());

let mut temp_list = LinkedList::new();
while let Some(val) = ip.pop_front() {
if val >= *sublist.back().unwrap() {
sublist.push_back(val);
} else {
temp_list.push_back(val);
}
}

let mut merged = LinkedList::new();
while let (Some(&op_val), Some(&sub_val)) = (op.front(), sublist.front()) {
if op_val <= sub_val {
merged.push_back(op.pop_front().unwrap());
} else {
merged.push_back(sublist.pop_front().unwrap());
}
}
merged.append(&mut op);
merged.append(&mut sublist);
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about creating two functions/procedures, something like collect_strands and merge_strands? This should improve the readability.

let mut merged = LinkedList::new();
while let (Some(&op_val), Some(&sub_val)) = (op.front(), sublist.front()) {
if op_val <= sub_val {
merged.push_back(op.pop_front().unwrap());
Copy link
Member

Choose a reason for hiding this comment

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

This line is not covered by any test.

Copy link

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Feb 25, 2024
Copy link

github-actions bot commented Mar 4, 2024

Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel. Thank you for your contributions!

@github-actions github-actions bot closed this Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants