-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added Rust implementation of bubble sort. #136
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
Conversation
I don't think we have many Rust people who can review PRs on a regular basis. Prepare for this one to last for a while. It will get merged eventually, though. |
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.
Thanks for the PR, I only have two small suggestions. Also @Butt4cak3, I can review Rust PRs :)
for _ in 0..n { | ||
for j in 1..n { | ||
if a[j - 1] > a[j] { | ||
// Swaps values at indices j - 1 and j |
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.
Rust has a swap
method on slices, I would use that instead of the explicit swap written here. It's faster and it's more descriptive
use rand::{thread_rng, Rng}; // Used for random number generation | ||
use rand::distributions::Uniform; // Used for a uniform distribution | ||
|
||
fn bubble_sort(a: &mut Vec<u32>) { |
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 should take &mut [u32]
since it doesn't use any functionality on Vec, and taking slices is strictly more general (for example it could allow you to partially sort a vector).
if a[j - 1] > a[j] { | ||
// Swaps values at indicies j - 1 and j | ||
a.swap(j, j - 1); | ||
a.swap(j, j - 1); // Swaps values at indicies j - 1 and j |
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.
doSomething(); // does something
I'm sorry. I had to. :D
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.
Yeah, this comment is not necessary
Added a Rust implementation of bubble sort using the
rand
crate as found at https://crates.io/crates/rand.If the random number generation for making a vector looks really esoteric, I'm willing to add another example with an easier-to-read sample vector!