pub fn radix_sort(arr: &mut [u64]) {
let max = match arr.iter().max() {
Some(&v) => v as usize,
None => return
};
let base = arr.len().next_power_of_two();
let mut truncate = 1;
while truncate <= max {
let base_order_index = |x| x as usize / truncate % base;
let mut orders = vec![0; base];
for &v in arr.iter() {
orders[base_order_index(v)] += 1;
}
for i in 1..base {
orders[i] += orders[i - 1];
}
for &v in arr.to_owned().iter().rev() {
let index = base_order_index(v);
orders[index] -= 1;
arr[orders[index]] = v;
}
truncate *= base;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結