pub fn counting_sort(arr: &mut [u32]) { | |
let max = arr.iter().max().unwrap(); | |
// 构建max+1数组,包含0 | |
let mut helper = vec![0; *max as usize + 1]; | |
// 计数 | |
for &value in arr.iter() { | |
helper[value as usize] += 1; | |
} | |
let mut index = 0; | |
for &value in helper.iter() { | |
// 添加重复值 | |
// 计数为0的自动跳过 | |
for _ in 0..value { | |
arr[index] = value; | |
index += 1; | |
} | |
} | |
} |
rust-algorithms:6-计数排序
Rust
404
0
0
2022-11-10
登录后可点赞和收藏
登录后可点赞和收藏