Skip to content

Commit

Permalink
Implement merge sort in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
opixelum committed Dec 7, 2023
1 parent 7f78985 commit 1208a6b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rust/src/sorting.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::arrays::{copy_arrays, merge_arrays};

pub fn insertion_sort<T: PartialOrd + Clone>(array: &mut [T]) {
let size = array.len();

Expand Down Expand Up @@ -81,6 +83,22 @@ pub fn cocktail_sort<T: PartialOrd + Clone>(array: &mut [T]) {
}
}

pub fn merge_sort<T: PartialOrd + Clone>(array: &mut [T]) {
let len = array.len();
if len > 1 {
let middle = array.len() / 2;
let (left, right) = array.split_at_mut(middle);

merge_sort(left);
merge_sort(right);

let mut temp = vec![left[0].clone(); len];
merge_arrays(left, right, &mut temp);

copy_arrays(&temp, array);
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -198,4 +216,29 @@ mod tests {
SORTED_STRING_ARRAY.iter().map(|&s| s.into()).collect();
assert_eq!(string_array, sorted_string_array);
}

#[test]
fn test_merge_sort() {
let mut int_array = INT_ARRAY;
merge_sort(&mut int_array);
assert_eq!(int_array, SORTED_INT_ARRAY);

let mut float_array = FLOAT_ARRAY;
merge_sort(&mut float_array);
assert_eq!(float_array, SORTED_FLOAT_ARRAY);

let mut char_array = CHAR_ARRAY;
merge_sort(&mut char_array);
assert_eq!(char_array, SORTED_CHAR_ARRAY);

let mut string_slice_array = STRING_SLICE_ARRAY;
merge_sort(&mut string_slice_array);
assert_eq!(string_slice_array, SORTED_STRING_SLICE_ARRAY);

let mut string_array: Vec<String> = STRING_ARRAY.iter().map(|&s| s.into()).collect();
merge_sort(&mut string_array);
let sorted_string_array: Vec<String> =
SORTED_STRING_ARRAY.iter().map(|&s| s.into()).collect();
assert_eq!(string_array, sorted_string_array);
}
}

0 comments on commit 1208a6b

Please sign in to comment.