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
pub fn selection_sort<'a, A: Ord + 'a>(data: &'a mut [A]) {
let (mut i, size) = (0, data.len());
while i < size {
let (mut x, mut current_min) = (i + 1, i);
while x < size {
if data[x] < data[current_min] {
current_min = x;
}
x += 1;
}
data.swap(i, current_min);
i += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_selection_sort() {
let mut data = vec![9, 8, 1, 5, 3, 16, 2, 0, 4];
selection_sort(data.as_mut_slice());
assert_eq!([0, 1, 2, 3, 4, 5, 8, 9, 16], data.as_slice());
}
}