-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Strand sort #666
Conversation
Codecov ReportAttention:
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. |
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.
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
src/sorting/strand_sort.rs
Outdated
return LinkedList::new(); | ||
} | ||
|
||
let mut ip = list.clone(); |
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.
Is this clone
really needed? Could we just have some index of active element in the (input) list
?
src/sorting/strand_sort.rs
Outdated
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()); |
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.
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)\ |
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.
* 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. |
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.
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. |
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); |
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.
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()); |
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.
This line is not covered by any test.
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. |
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! |
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.
Checklist:
cargo clippy --all -- -D warnings
just before my last commit and fixed any issue that was found.cargo fmt
just before my last commit.cargo test
just before my last commit and all tests passed.mod.rs
file within its own folder, and in any parent folder(s).DIRECTORY.md
with the correct link.COUNTRIBUTING.md
and my code follows its guidelines.