0%

230329--选择排序

题目

写一个选择排序

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn selection_sort(arr: Vec<i32>) -> Vec<i32> {
let mut min = std::i32::MIN;
let mut result = vec![];
for _i in &arr {
let c_min = find_small(&arr, min);
min = c_min;
result.push(c_min);
}
result
}
fn find_small(arr: &Vec<i32>, min: i32) -> i32 {
let mut c_min = std::i32::MAX;
for i in arr {
if *i <= min {
continue;
}
if *i < c_min {
c_min = *i;
}
}
c_min
}