Skip to content

Commit

Permalink
add merge-sort with tests (TheAlgorithms#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinbhojwani authored Apr 6, 2020
1 parent 1c388c6 commit 6444eb1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ These are for demonstration purposes only.
- [Counting](./src/sorting/counting_sort.rs)
- [Heap](./src/sorting/heap_sort.rs)
- [Insertion](./src/sorting/insertion_sort.rs)
- Merge _(Not implemented yet)_
- [Merge](./src/sorting/merge_sort.rs)
- [Quick](./src/sorting/quick_sort.rs)
- Radix _(Not implemented yet)_
- [Selection](./src/sorting/selection_sort.rs)
Expand Down
2 changes: 1 addition & 1 deletion src/sorting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ __Properties__
###### View the algorithm in [action][insertion-toptal]


### Merge _(Not implemented yet)_
### [Merge](./merge_sort.rs)
![alt text][merge-image]

From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
Expand Down
107 changes: 107 additions & 0 deletions src/sorting/merge_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
fn _merge<T: Ord + Copy>(arr: &mut [T], lo: usize, mid: usize, hi: usize) {
// create temporary arrays to support merge
let mut left_half = Vec::new();
let mut right_half = Vec::new();
for i in lo..mid + 1 {
left_half.push(arr[i]);
}
for i in mid + 1..hi + 1 {
right_half.push(arr[i]);
}

let lsize = left_half.len();
let rsize = right_half.len();

// pointers to track the positions while merging
let mut l = 0;
let mut r = 0;
let mut a = lo;

// pick smaller element one by one from either left or right half
while l < lsize && r < rsize {
if left_half[l] < right_half[r] {
arr[a] = left_half[l];
l += 1;
} else {
arr[a] = right_half[r];
r += 1;
}
a += 1;
}

// put all the remaining ones
while l < lsize {
arr[a] = left_half[l];
l += 1;
a += 1;
}

while r < rsize {
arr[a] = right_half[r];
r += 1;
a += 1;
}
}

fn _merge_sort<T: Ord + Copy>(arr: &mut [T], lo: usize, hi: usize) {
if lo < hi {
let mid = lo + (hi - lo) / 2;
_merge_sort(arr, lo, mid);
_merge_sort(arr, mid + 1, hi);
_merge(arr, lo, mid, hi);
}
}

pub fn merge_sort<T: Ord + Copy>(arr: &mut [T]) {
let len = arr.len();
if len > 1 {
_merge_sort(arr, 0, len - 1);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn basic() {
let mut res = vec![10, 8, 4, 3, 1, 9, 2, 7, 5, 6];
merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}

#[test]
fn basic_string() {
let mut res = vec!["a", "bb", "d", "cc"];
merge_sort(&mut res);
assert_eq!(res, vec!["a", "bb", "cc", "d"]);
}

#[test]
fn empty() {
let mut res = Vec::<u8>::new();
merge_sort(&mut res);
assert_eq!(res, vec![]);
}

#[test]
fn one_element() {
let mut res = vec![1];
merge_sort(&mut res);
assert_eq!(res, vec![1]);
}

#[test]
fn pre_sorted() {
let mut res = vec![1, 2, 3, 4];
merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}

#[test]
fn reverse_sorted() {
let mut res = vec![4, 3, 2, 1];
merge_sort(&mut res);
assert_eq!(res, vec![1, 2, 3, 4]);
}
}
2 changes: 2 additions & 0 deletions src/sorting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod bubble_sort;
mod counting_sort;
mod heap_sort;
mod insertion;
mod merge_sort;
mod quick_sort;
mod selection_sort;

Expand All @@ -12,6 +13,7 @@ pub use self::counting_sort::counting_sort;
pub use self::counting_sort::generic_counting_sort;
pub use self::heap_sort::heap_sort;
pub use self::insertion::insertion_sort;
pub use self::merge_sort::merge_sort;
pub use self::quick_sort::quick_sort;
pub use self::selection_sort::selection_sort;

Expand Down

0 comments on commit 6444eb1

Please sign in to comment.