rust-algorithms:13-选择排序
pub fn selection_sort<T: PartialOrd>(arr: &mut [T]) {
let len = arr.len();
if len > 1 {
for current_index in 0..len - 1 {
let mut min_index = current_index;
// 查找最小值
for compare_index in current_index+1..len {
if arr[min_index] > arr[compare_index] {
min_index = compare_index;
}
}
// 把最小值设置为当前值
if min_index != current_index {
arr.swap(current_index, min_index);
}
}
}
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: