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 協議》,轉載必須註明作者和本文連結