| 1 | use crate::sorting::traits::Sorter; |
| 2 | |
| 3 | fn selection_sort<T: Ord>(array: &mut [T]) { |
| 4 | // Loop through each element in the array. |
| 5 | for i in 0..array.len() { |
| 6 | // The current element is the starting minimum element. |
| 7 | let mut smallest_idx = i; |
| 8 | // Loop through the remaining elements, if any of them is less than the current minimum, we update the current minimum. |
| 9 | for j in i + 1..array.len() { |
| 10 | if array[j] < array[smallest_idx] { |
| 11 | smallest_idx = j; |
| 12 | } |
| 13 | } |
| 14 | // We can then swap the minimum element with the current element. |
| 15 | array.swap(i, smallest_idx); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub struct SelectionSort; |
| 20 | |