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 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!