pub struct RadixSort<T>where
T: RadixSortable,{ /* private fields */ }Expand description
Universal Radix Sort implementation.
This struct provides a generic interface for sorting collections using the Radix Sort algorithm. It supports multiple data types including integers, floating-point numbers, and strings.
§Type Parameters
T- The type of elements to sort (must implementRadixSortable)
§Examples
use universal_radix_sort::{RadixSort, RadixDataType, SortDirection};
// Sort integers in ascending order
let mut numbers = vec![5, 2, 8, 1, 9];
let sorter = RadixSort::<i64>::new(RadixDataType::SignedInteger, SortDirection::Ascending);
sorter.sort(&mut numbers);
assert_eq!(numbers, vec![1, 2, 5, 8, 9]);
// Sort in descending order
let mut numbers = vec![5, 2, 8, 1, 9];
let sorter = RadixSort::<i64>::new(RadixDataType::SignedInteger, SortDirection::Descending);
sorter.sort(&mut numbers);
assert_eq!(numbers, vec![9, 8, 5, 2, 1]);§Performance
- Time Complexity: O(n·k) where n is the number of elements and k is the number of bytes
- Space Complexity: O(n) for the temporary buffer
§Stability
Radix Sort is a stable sorting algorithm, meaning that equal elements maintain their relative order after sorting.
Implementations§
Source§impl RadixSort<String>
impl RadixSort<String>
Sourcepub fn sort_strings_msd(&self, slice: &mut [String]) -> RadixResult<()>
pub fn sort_strings_msd(&self, slice: &mut [String]) -> RadixResult<()>
Source§impl RadixSort<&str>
impl RadixSort<&str>
Sourcepub fn sort_str_slice_msd(&self, slice: &mut [&str]) -> RadixResult<()>
pub fn sort_str_slice_msd(&self, slice: &mut [&str]) -> RadixResult<()>
Source§impl<T> RadixSort<T>where
T: RadixSortable,
impl<T> RadixSort<T>where
T: RadixSortable,
Sourcepub fn new(data_type: RadixDataType, direction: SortDirection) -> Self
pub fn new(data_type: RadixDataType, direction: SortDirection) -> Self
Creates a new RadixSort instance.
§Arguments
data_type- The data type category for sorting strategy selectiondirection- The sort direction (ascending or descending)
§Returns
A new RadixSort instance configured with the specified parameters
§Examples
use universal_radix_sort::{RadixSort, RadixDataType, SortDirection};
let sorter = RadixSort::<i64>::new(
RadixDataType::SignedInteger,
SortDirection::Ascending,
);Sourcepub fn with_processing_order(
data_type: RadixDataType,
direction: SortDirection,
processing_order: ProcessingOrder,
) -> Self
pub fn with_processing_order( data_type: RadixDataType, direction: SortDirection, processing_order: ProcessingOrder, ) -> Self
Creates a new RadixSort instance with custom processing order.
§Arguments
data_type- The data type categorydirection- The sort directionprocessing_order- The byte processing order
§Examples
use universal_radix_sort::{RadixSort, RadixDataType, SortDirection, ProcessingOrder};
let sorter = RadixSort::<String>::with_processing_order(
RadixDataType::String,
SortDirection::Ascending,
ProcessingOrder::MsbFirst,
);Sourcepub fn sort(&self, slice: &mut [T]) -> RadixResult<()>
pub fn sort(&self, slice: &mut [T]) -> RadixResult<()>
Sorts a mutable slice in-place.
This method modifies the input slice directly without allocating a new collection.
§Arguments
slice- The mutable slice to sort
§Returns
Ok(()) on success, Err(RadixError) if sorting fails
§Examples
use universal_radix_sort::{RadixSort, RadixDataType, SortDirection};
let mut numbers = vec![5, 2, 8, 1, 9];
let sorter = RadixSort::<i64>::new(RadixDataType::SignedInteger, SortDirection::Ascending);
sorter.sort(&mut numbers).unwrap();
assert_eq!(numbers, vec![1, 2, 5, 8, 9]);§Performance
- Allocates O(n) temporary buffer for stability
- Does not allocate a new collection
Sourcepub fn sort_to_vec(&self, collection: &[T]) -> RadixResult<Vec<T>>
pub fn sort_to_vec(&self, collection: &[T]) -> RadixResult<Vec<T>>
Sorts a collection and returns a new sorted vector.
This method does not modify the input collection.
§Arguments
collection- The collection to sort
§Returns
A new sorted vector, or Err(RadixError) if sorting fails
§Examples
use universal_radix_sort::{RadixSort, RadixDataType, SortDirection};
let numbers = vec![5, 2, 8, 1, 9];
let sorter = RadixSort::<i64>::new(RadixDataType::SignedInteger, SortDirection::Ascending);
let sorted = sorter.sort_to_vec(&numbers).unwrap();
assert_eq!(sorted, vec![1, 2, 5, 8, 9]);
// Original is unchanged
assert_eq!(numbers, vec![5, 2, 8, 1, 9]);Sourcepub fn data_type(&self) -> RadixDataType
pub fn data_type(&self) -> RadixDataType
Returns the configured data type.
Sourcepub fn direction(&self) -> SortDirection
pub fn direction(&self) -> SortDirection
Returns the configured sort direction.
Sourcepub fn processing_order(&self) -> ProcessingOrder
pub fn processing_order(&self) -> ProcessingOrder
Returns the configured processing order.