diff --git a/algorithms/Rust/README.md b/algorithms/Rust/README.md index 2f71bf2d..74ac70eb 100644 --- a/algorithms/Rust/README.md +++ b/algorithms/Rust/README.md @@ -1,4 +1,5 @@ # Rust -1. [Palindrome Check](strings/palindrome/README.md) \ No newline at end of file +1. [Palindrome Check](strings/palindrome/README.md) +2. [Merge Sort](sorting/merge-sort/README.md) diff --git a/algorithms/Rust/sorting/merge-sort/.gitignore b/algorithms/Rust/sorting/merge-sort/.gitignore new file mode 100644 index 00000000..3cdf0067 --- /dev/null +++ b/algorithms/Rust/sorting/merge-sort/.gitignore @@ -0,0 +1,4 @@ +### Rust +Cargo.lock +# Rust compiled output +target diff --git a/algorithms/Rust/sorting/merge-sort/Cargo.toml b/algorithms/Rust/sorting/merge-sort/Cargo.toml new file mode 100644 index 00000000..1b974307 --- /dev/null +++ b/algorithms/Rust/sorting/merge-sort/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "merge-sort" +version = "0.1.0" +authors = ["Akshat Adsule <30205531+AkshatAdsule@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" \ No newline at end of file diff --git a/algorithms/Rust/sorting/merge-sort/README.md b/algorithms/Rust/sorting/merge-sort/README.md new file mode 100644 index 00000000..b4f587ff --- /dev/null +++ b/algorithms/Rust/sorting/merge-sort/README.md @@ -0,0 +1,15 @@ +This sorts an array using Merge Sort. The sort is generic and will work with with any `Vec` whose items have the `std::cmp::Ord ` and `Copy` traits. More information about Merge Sort can be found [here](https://en.wikipedia.org/wiki/Merge_sort) + +## Prerequisites + +- Rust (https://rust-lang.org) + +## Instructions for running the project + +- Open a terminal/powershell window in the folder/directory. + +- Run the following command + + ```bash + cargo run --release + ``` diff --git a/algorithms/Rust/sorting/merge-sort/src/main.rs b/algorithms/Rust/sorting/merge-sort/src/main.rs new file mode 100644 index 00000000..6f8b8926 --- /dev/null +++ b/algorithms/Rust/sorting/merge-sort/src/main.rs @@ -0,0 +1,66 @@ +use rand::{distributions::Uniform, Rng}; + +fn main() { + // Create random numbers + let range = Uniform::from(0..=100); + let values: Vec = rand::thread_rng().sample_iter(&range).take(100).collect(); + println!("Sorting: {:?}",values); + let result = merge_sort(values); + println!("Sorted: {:?}", result); +} + +// Limit type to only objects that can be ordered, cloned and copied +fn merge_sort(nums: Vec) -> Vec { + // If array only has one item, it is already sorted :-) + if nums.len() == 1 { + return vec![nums[0]]; + } + + // If array has 2 items, just compare the values and sort + if nums.len() == 2 { + if nums[0] <= nums[1] { + return vec![nums[0], nums[1]]; + } else { + return vec![nums[1], nums[0]]; + } + } + + // Split into 2 + let half = nums.len() / 2; + let mut left = merge_sort((&nums[0..half]).to_vec()); + let mut right = merge_sort((&nums[half..]).to_vec()); + + let mut sorted: Vec = Vec::new(); + + loop { + // If either array is empty, the values in the other array must be larger + // and so we should dump the remaining array's content into the sorted array + if left.is_empty() { + for i in right { + sorted.push(i); + } + break; + } + if right.is_empty() { + for i in left { + sorted.push(i); + } + break; + } + + // Merge arrays + if left[0] >= right[0] { + sorted.push(right[0]); + right.remove(0); + continue; + } + if left[0] <= right[0] { + sorted.push(left[0]); + left.remove(0); + continue; + } + } + + // return sorted array + sorted +}