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

Added BogoBogo Sort, Tree Sort, Bitonic Sort, Pigeonhole Sort, Derrivative Method Alogorithms #67

Merged
merged 3 commits into from
Jan 21, 2024

Conversation

Github-Vektor
Copy link
Contributor

@Github-Vektor Github-Vektor commented Jan 20, 2024

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.

@Github-Vektor Github-Vektor changed the title Added BogoBogo Sort Added BogoBogo Sort, Tree Sort, Bitonic Sort, Pigeonhole Sort, Derrivative Method Alogorithms Jan 21, 2024
Copy link
Owner

@alexfertel alexfertel left a 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!

where
F: Fn(f64, f64) -> f64,
{
let h = 0.0001;
Copy link
Owner

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.

Comment on lines 9 to 11
fn test_function(x: f64, y: f64) -> f64 {
x.powi(2) + y.powi(2)
}
Copy link
Owner

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.

Comment on lines 13 to 21
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);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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;
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
mod derrivative_method;
mod derivative_method;

Note that the file + several occurrences of derivative have this typo.

@@ -35,18 +37,23 @@ mod insertion_sort;
mod merge_sort;
mod odd_even_sort;
mod pancake_sort;
mod pigenhole_sort;
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
mod pigenhole_sort;
mod pigeonhole_sort;

Comment on lines 30 to 35
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);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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);
}

Comment on lines 41 to 52
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!();
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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!();
}

Comment on lines 33 to 44
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);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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);
}

Comment on lines 63 to 71
fn main() {
let mut arr = vec![5, 4, 7, 2, 11];
tree_sort(&mut arr);
for item in arr.iter() {
print!("{} ", item);
}

println!();
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
fn main() {
let mut arr = vec![5, 4, 7, 2, 11];
tree_sort(&mut arr);
for item in arr.iter() {
print!("{} ", item);
}
println!();
}

@alexfertel alexfertel merged commit a2c1c89 into alexfertel:main Jan 21, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants