Skip to main content

RadixSort

Struct RadixSort 

Source
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 implement RadixSortable)

§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>

Source

pub fn sort_strings_msd(&self, slice: &mut [String]) -> RadixResult<()>

MSD (Most Significant Digit) first radix sort for String.

This is more efficient for strings as it allows early termination when differences are found in higher-order characters.

§Arguments
  • slice - The mutable slice of Strings to sort
§Returns

Ok(()) on success, Err(RadixError) if sorting fails.

Source§

impl RadixSort<&str>

Source

pub fn sort_str_slice_msd(&self, slice: &mut [&str]) -> RadixResult<()>

MSD radix sort for &str slice.

§Arguments
  • slice - The mutable slice to sort
§Returns

Ok(()) on success.

Source§

impl<T> RadixSort<T>
where T: RadixSortable,

Source

pub fn new(data_type: RadixDataType, direction: SortDirection) -> Self

Creates a new RadixSort instance.

§Arguments
  • data_type - The data type category for sorting strategy selection
  • direction - 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,
);
Source

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 category
  • direction - The sort direction
  • processing_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,
);
Source

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
Source

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]);
Source

pub fn data_type(&self) -> RadixDataType

Returns the configured data type.

Source

pub fn direction(&self) -> SortDirection

Returns the configured sort direction.

Source

pub fn processing_order(&self) -> ProcessingOrder

Returns the configured processing order.

Trait Implementations§

Source§

impl<T> Default for RadixSort<T>
where T: RadixSortable,

Source§

fn default() -> Self

Creates a default RadixSort with SignedInteger type and Ascending direction.

Auto Trait Implementations§

§

impl<T> Freeze for RadixSort<T>

§

impl<T> RefUnwindSafe for RadixSort<T>
where T: RefUnwindSafe,

§

impl<T> Send for RadixSort<T>
where T: Send,

§

impl<T> Sync for RadixSort<T>
where T: Sync,

§

impl<T> Unpin for RadixSort<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for RadixSort<T>

§

impl<T> UnwindSafe for RadixSort<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.