-
Notifications
You must be signed in to change notification settings - Fork 5
/
selection_sort.rs
54 lines (43 loc) · 1.15 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
fn find_smallest<T>(collection: &Vec<T>) -> usize
where
T: std::cmp::Ord + std::fmt::Display,
{
let mut smallest_index: usize = 0;
let mut smallest_value: &T = &collection[smallest_index];
for (idx, el) in collection.iter().enumerate() {
if el < smallest_value {
smallest_index = idx;
smallest_value = el;
}
}
smallest_index
}
#[allow(dead_code)]
pub fn selection_sort<T>(collection: &Vec<T>) -> Vec<T>
where
T: std::cmp::Ord + std::fmt::Display + std::clone::Clone,
{
let mut result: Vec<T> = Vec::new();
let mut copy = collection.clone();
for _ in 0..collection.len() {
let smallest = find_smallest(©);
result.push(copy.remove(smallest));
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sorts_a_collection() {
let items = vec![8, 2, 4, 6, 5, 7, 10];
let result = selection_sort(&items);
assert_eq!(result, vec![2, 4, 5, 6, 7, 8, 10]);
}
#[test]
fn finds_smallest() {
let items = vec![3, 5, 7, -1, 8, 40, 1];
let result = find_smallest(&items);
assert_eq!(result, 3);
}
}