pub fn merge_sort(arr: &mut [i32])
Expand description
Sorts a slice using the Merge Sort algorithm.
Merge sort is a divide-and-conquer algorithm that splits the array into two halves, recursively sorts them, and then merges the sorted halves.
§Parameters
arr
: A mutable reference to the slice of integers to be sorted.
§Time Complexity
- Best:
O(n log n)
- Worst:
O(n log n)
- Average:
O(n log n)
§Space Complexity
O(n)
(requires additional space for merging)
§Examples
use dsa::algorithms::sorting::merge_sort;
let mut arr = [5, 3, 8, 4, 2];
merge_sort(&mut arr);
assert_eq!(arr, [2, 3, 4, 5, 8]);