-
Notifications
You must be signed in to change notification settings - Fork 76
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
Added BogoBogo Sort, Tree Sort, Bitonic Sort, Pigeonhole Sort, Derrivative Method Alogorithms #67
Conversation
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 your contribution! ❤️
Only a few minor tweaks and this is good to go!
src/math/derrivative_method.rs
Outdated
where | ||
F: Fn(f64, f64) -> f64, | ||
{ | ||
let h = 0.0001; |
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.
nit: Extract constant. const DERIVATIVE_PRECISION: f64 = 0.0001
.
src/math/derrivative_method.rs
Outdated
fn test_function(x: f64, y: f64) -> f64 { | ||
x.powi(2) + y.powi(2) | ||
} |
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.
Move to the tests
mod.
src/math/derrivative_method.rs
Outdated
fn main() { | ||
let x = 1.0; | ||
let y = 2.0; | ||
let f = test_function; | ||
let df_dx = derivative_method(x, y, f); | ||
let df_dy = derivative_method(y, x, f); | ||
println!("df/dx = {}", df_dx); | ||
println!("df/dy = {}", df_dy); | ||
} |
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.
fn main() { | |
let x = 1.0; | |
let y = 2.0; | |
let f = test_function; | |
let df_dx = derivative_method(x, y, f); | |
let df_dy = derivative_method(y, x, f); | |
println!("df/dx = {}", df_dx); | |
println!("df/dy = {}", df_dy); | |
} |
src/math/mod.rs
Outdated
@@ -1,5 +1,6 @@ | |||
mod armstrong_number; | |||
mod baby_step_giant_step; | |||
mod derrivative_method; |
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.
mod derrivative_method; | |
mod derivative_method; |
Note that the file + several occurrences of derivative have this typo.
src/sorting/mod.rs
Outdated
@@ -35,18 +37,23 @@ mod insertion_sort; | |||
mod merge_sort; | |||
mod odd_even_sort; | |||
mod pancake_sort; | |||
mod pigenhole_sort; |
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.
mod pigenhole_sort; | |
mod pigeonhole_sort; |
src/sorting/pigenhole_sort.rs
Outdated
fn main() { | ||
let mut arr = vec![10, 10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1]; | ||
pigeonhole_sort(&mut arr); | ||
|
||
println!("Sorted order is : {:?}", arr); | ||
} |
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.
fn main() { | |
let mut arr = vec![10, 10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1]; | |
pigeonhole_sort(&mut arr); | |
println!("Sorted order is : {:?}", arr); | |
} |
src/sorting/strand_sort.rs
Outdated
fn main() { | ||
let mut ip: LinkedList<i32> = LinkedList::from([10, 5, 30, 40, 2, 4, 9]); | ||
let mut op: LinkedList<i32> = LinkedList::new(); | ||
|
||
strand_sort(&mut ip, &mut op); | ||
|
||
for x in op { | ||
print!("{} ", x); | ||
} | ||
|
||
println!(); | ||
} |
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.
fn main() { | |
let mut ip: LinkedList<i32> = LinkedList::from([10, 5, 30, 40, 2, 4, 9]); | |
let mut op: LinkedList<i32> = LinkedList::new(); | |
strand_sort(&mut ip, &mut op); | |
for x in op { | |
print!("{} ", x); | |
} | |
println!(); | |
} |
src/sorting/bitonic_sort.rs
Outdated
fn main() { | ||
let mut numbers = vec![10, 30, 11, 20, 4, 330, 21, 110]; | ||
// Ensure the length is a power of 2 for bitonic sorting | ||
assert_eq!( | ||
numbers.len() & (numbers.len() - 1), | ||
0, | ||
"The length of the array should be a power of 2." | ||
); | ||
|
||
bitonic_sort(true, &mut numbers); | ||
println!("Sorted numbers: {:?}", numbers); | ||
} |
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.
fn main() { | |
let mut numbers = vec![10, 30, 11, 20, 4, 330, 21, 110]; | |
// Ensure the length is a power of 2 for bitonic sorting | |
assert_eq!( | |
numbers.len() & (numbers.len() - 1), | |
0, | |
"The length of the array should be a power of 2." | |
); | |
bitonic_sort(true, &mut numbers); | |
println!("Sorted numbers: {:?}", numbers); | |
} |
src/sorting/tree_sort.rs
Outdated
fn main() { | ||
let mut arr = vec![5, 4, 7, 2, 11]; | ||
tree_sort(&mut arr); | ||
for item in arr.iter() { | ||
print!("{} ", item); | ||
} | ||
|
||
println!(); | ||
} |
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.
fn main() { | |
let mut arr = vec![5, 4, 7, 2, 11]; | |
tree_sort(&mut arr); | |
for item in arr.iter() { | |
print!("{} ", item); | |
} | |
println!(); | |
} |
Description
This pull request introduces the implementation of several sorting algorithms in Rust, enhancing our collection with both efficient and deliberately inefficient sorting methods. Included in this update are implementations of Tree Sort, Bitonic Sort, Strand Sort, and the notably inefficient BogoBogo Sort. Each algorithm showcases different sorting techniques and their respective advantages and limitations.
Algorithm Descriptions
Tree Sort
Tree Sort leverages the properties of a binary search tree to sort an array. It involves building a binary search tree from the array elements and then performing an in-order traversal to obtain a sorted array. This method is efficient for datasets that are not overly large and provides an average-case time complexity of O(n log n).
Bitonic Sort
Bitonic Sort is an efficient parallel sorting algorithm, well-suited for multi-threading and distributed systems. It is particularly efficient for data sets sized as a power of two. The algorithm works by first sorting sub-arrays in different directions and then merging them in a bitonic sequence. Its time complexity is O(n log² n).
Strand Sort
Strand Sort is an intuitive and adaptive sorting algorithm, ideal for partially sorted lists. It repeatedly extracts sorted sublists (strands) from the unsorted list and merges them into the sorted part. While not the most efficient for all cases, its average and worst-case time complexity is typically O(n²).
BogoBogo Sort
BogoBogo Sort is a highly inefficient and theoretical sorting algorithm, based on the generate-and-test paradigm. It recursively sorts the first n-1 elements of an array and then checks if the nth element is in place. If not, it shuffles the entire array and repeats the process. This implementation uses Rust's array manipulation features and the
rand
crate for element shuffling.Implementation Method
Each algorithm is implemented in Rust, utilizing its powerful features for memory safety, recursion, and array manipulation. The implementations also include tests to verify functionality. However, given the inefficiency of BogoBogo Sort, its tests are limited to small arrays.
Each of these sorting algorithms demonstrates unique characteristics and performance implications, making them valuable additions to our collection of sorting algorithms.
Sources and References
Note: The pull request includes detailed documentation for each algorithm, along with the Rust code and unit tests. The inclusion of BogoBogo Sort, while impractical, serves as an educational tool to understand sorting algorithm efficiency and design.