pub fn selection_sort(arr: &mut [i32])
Expand description
Sorts a slice using the Selection Sort algorithm.
Selection sort repeatedly selects the minimum element from the unsorted portion of the array and swaps it with the first unsorted element.
§Parameters
arr
: A mutable reference to the slice of integers to be sorted.
§Time Complexity
- Best:
O(n²)
- Worst:
O(n²)
- Average:
O(n²)
§Space Complexity
O(1)
(in-place sorting)
§Examples
use dsa::algorithms::sorting::selection_sort;
let mut arr = [5, 3, 8, 4, 2];
selection_sort(&mut arr);
assert_eq!(arr, [2, 3, 4, 5, 8]);