-
Notifications
You must be signed in to change notification settings - Fork 73
/
selection_sort.rs
37 lines (32 loc) · 1.03 KB
/
selection_sort.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::sorting::traits::Sorter;
fn selection_sort<T: Ord>(array: &mut [T]) {
// Loop through each element in the array.
for i in 0..array.len() {
// The current element is the starting minimum element.
let mut smallest_idx = i;
// Loop through the remaining elements, if any of them is less than the current minimum, we update the current minimum.
for j in i + 1..array.len() {
if array[j] < array[smallest_idx] {
smallest_idx = j;
}
}
// We can then swap the minimum element with the current element.
array.swap(i, smallest_idx);
}
}
pub struct SelectionSort;
impl<T> Sorter<T> for SelectionSort
where
T: Ord + Copy,
{
fn sort_inplace(array: &mut [T]) {
selection_sort(array);
}
}
#[cfg(test)]
mod tests {
use crate::sorting::traits::Sorter;
use crate::sorting::SelectionSort;
sorting_tests!(SelectionSort::sort, selection_sort);
sorting_tests!(SelectionSort::sort_inplace, selection_sort, inplace);
}