sorting_algorithm/
selection_sort.rs

1/// Sorts a data set using Selection Sort
2///
3/// Average time complexity: O(n<sup>2</sup>)
4///
5/// # Examples
6///
7/// ```
8/// use sorting_algorithm::selection_sort;
9///
10/// fn main() {
11///     let mut data = [3, 1, 2, 5, 4];
12///     
13///     selection_sort::sort(&mut data);
14///
15///     assert_eq!(data, [1, 2, 3, 4, 5]);
16/// }
17/// ```
18pub fn sort<T: Ord>(data: &mut [T]) {
19    let len = data.len();
20
21    for i in 0..len {
22        let mut min_index = i;
23
24        for j in i + 1..len {
25            if data[j] < data[min_index] {
26                min_index = j;
27            }
28        }
29
30        data.swap(i, min_index);
31    }
32}