MCPcopy Create free account
hub / github.com/alexfertel/rust-algorithms / selection_sort

Function selection_sort

src/sorting/selection_sort.rs:3–17  ·  view source on GitHub ↗
(array: &mut [T])

Source from the content-addressed store, hash-verified

1use crate::sorting::traits::Sorter;
2
3fn 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
19pub struct SelectionSort;
20

Callers 1

sort_inplaceMethod · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected