use std::convert::{TryFrom, TryInto};
pub fn sort<T>(array: &mut [T], radix: usize) -> Result<(), String>
where
T: TryInto<isize> + TryFrom<isize> + std::clone::Clone,
<T as TryInto<isize>>::Error: std::fmt::Debug,
{
_radix_sort_scan_impl(array, radix, true)
}
pub fn sort_reverse<T>(array: &mut [T], radix: usize) -> Result<(), String>
where
T: TryInto<isize> + TryFrom<isize> + std::clone::Clone,
<T as TryInto<isize>>::Error: std::fmt::Debug,
{
_radix_sort_scan_impl(array, radix, false)
}
fn _radix_sort_impl<T>(
array: &mut [T],
digits_max: usize,
radix: usize,
asc: bool,
) -> Result<(), String>
where
T: TryInto<isize> + TryFrom<isize> + std::clone::Clone,
<T as TryInto<isize>>::Error: std::fmt::Debug,
{
use std::collections::LinkedList;
let mut counter = vec![LinkedList::new(); radix];
let mut neg_counter = vec![LinkedList::new(); radix];
for y in 0..digits_max {
for j in 0..array.len() {
let mut e = array[j].to_owned().try_into().unwrap();
let is_neg = e < 0;
if is_neg {
e = e.abs();
}
let modulo = radix.pow(y as u32 + 1) as isize;
let divisor = modulo / radix as isize;
let index = e % modulo / divisor;
if is_neg {
neg_counter[index as usize].push_back(array[j].to_owned());
} else {
counter[index as usize].push_back(array[j].to_owned());
}
}
let mut pos = 0;
if asc {
for i in 0_isize..(neg_counter.len() as isize) {
while let Some(value) = neg_counter[i as usize].pop_back() {
array[pos] = value;
pos += 1;
}
}
for i in 0_isize..(counter.len() as isize) {
while let Some(value) = counter[i as usize].pop_front() {
array[pos] = value;
pos += 1;
}
}
} else {
for i in (0_isize..(counter.len() as isize)).rev() {
while let Some(value) = counter[i as usize].pop_front() {
array[pos] = value;
pos += 1;
}
}
for i in (0_isize..(neg_counter.len() as isize)).rev() {
while let Some(value) = neg_counter[i as usize].pop_back() {
array[pos] = value;
pos += 1;
}
}
}
}
Ok(())
}
fn _radix_sort_scan_impl<T>(array: &mut [T], radix: usize, asc: bool) -> Result<(), String>
where
T: TryInto<isize> + TryFrom<isize> + std::clone::Clone,
<T as TryInto<isize>>::Error: std::fmt::Debug,
{
if array.len() == 0 {
return Ok(());
}
let mut abs_max: isize = array[0].to_owned().try_into().unwrap().abs();
for e in array.iter() {
let e: isize = e.to_owned().try_into().unwrap().abs();
if e > abs_max {
abs_max = e;
}
}
let abs_max = abs_max as f64;
let digits_max = abs_max.log(radix as f64) as usize + 1;
return _radix_sort_impl(array, digits_max, radix, asc);
}
mod tests {
#[test]
fn sort_ascending() {
struct TestCase {
input: Vec<i32>,
expected: Vec<i32>,
}
let test_cases = vec![TestCase {
input: vec![1, 4, 2, 3, 5, 111, 234, 21, 13],
expected: vec![1, 2, 3, 4, 5, 13, 21, 111, 234],
}];
for case in test_cases {
let mut actual = case.input.clone();
super::sort(&mut actual, 10).unwrap();
assert_eq!(actual, case.expected);
}
}
#[test]
fn sort_descending() {
struct TestCase {
input: Vec<i32>,
expected: Vec<i32>,
}
let test_cases = vec![TestCase {
input: vec![1, 4, 2, 3, 5, 111, 234, 21, 13],
expected: vec![234, 111, 21, 13, 5, 4, 3, 2, 1],
}];
for case in test_cases {
let mut actual = case.input.clone();
super::sort_reverse(&mut actual, 10).unwrap();
assert_eq!(actual, case.expected);
}
}
}