#[repr(transparent)]pub struct Sort<T>(pub T);
Expand description
🌐
Provides sorting methods for arrays and slices of T
, some of them const.
It implements the following methods for sorting exclusive slices:
bubble
,
counting
,
counting_buf
,
insertion
,
merge
,
quick_lomuto
,
quick_hoare
,
quick_3way
,
quick_selection
,
quick_shaker
.
§Features
When the corresponding feature _sort_f[32|64]
or _sort_[iu][8|16|32|64|128]
is enabled,
It implements the following const methods for sorting copied arrays of primitives:
bubble_array
, insertion_array
, selection_array
.
In the case of floating-point primitives it uses total ordering.
§Examples
Sort copied arrays of primitives:
let mut arr = [4i32, 7, -5, 1, -13, 0]; // signed primitives
assert_eq![Sort(arr).bubble_array(), [-13, -5, 0, 1, 4, 7]];
assert_eq![Sort(arr).insertion_array(), [-13, -5, 0, 1, 4, 7]];
assert_eq![Sort(arr).selection_array(), [-13, -5, 0, 1, 4, 7]];
let mut arr = [4u32, 7, 5, 1, 13, 0]; // unsigned primitives
assert_eq![Sort(arr).bubble_array(), [0, 1, 4, 5, 7, 13]];
assert_eq![Sort(arr).insertion_array(), [0, 1, 4, 5, 7, 13]];
assert_eq![Sort(arr).selection_array(), [0, 1, 4, 5, 7, 13]];
let mut arr = [4.01f32, 7.9, -5.4, 1.0, 0.0, -0.0]; // floating-point primitives
assert_eq![Sort(arr).bubble_array(), [-5.4, -0.0, 0.0, 1.0, 4.01, 7.9]];
assert_eq![Sort(arr).insertion_array(), [-5.4, -0.0, 0.0, 1.0, 4.01, 7.9]];
assert_eq![Sort(arr).selection_array(), [-5.4, -0.0, 0.0, 1.0, 4.01, 7.9]];
§Performance
The _array
suffixed methods calls the cswap
macro using the xor swap
algorithm, except for the floting-point version which uses a temporary variable.
Tuple Fields§
§0: T
Implementations§
Source§impl<T: Ord> Sort<&mut [T]>
impl<T: Ord> Sort<&mut [T]>
Sourcepub fn bubble(self)
pub fn bubble(self)
Sorts a slice using bubble sort.
§Examples
let mut data = [4, 7, -5, 1, -13, 0];
Sort(&mut data[..]).bubble();
assert_eq![data, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn counting(self) -> Vec<usize> ⓘwhere
T: Clone,
Available on crate feature alloc
only.
pub fn counting(self) -> Vec<usize> ⓘwhere
T: Clone,
alloc
only.Sorts a slice using counting sort, and returns the ordered frequencies.
Counting sort is particularly efficient when the range of input values is small compared to the number of elements to be sorted.
§Examples
let mut data = [4, 64, 4, 2, 4, 8, 8, 4, 8, 4, 2, 8, 64, 4, 8, 4, 2];
let freq = Sort(&mut data[..]).counting();
assert_eq![data, [2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 64, 64]];
assert_eq![freq, [3, 7, 5, 2]];
Sourcepub fn counting_buf(self, freq: &mut [T], values: &[T]) -> Option<()> ⓘ
pub fn counting_buf(self, freq: &mut [T], values: &[T]) -> Option<()> ⓘ
Sorts a slice using counting sort, and writes the frequencies, without allocating.
Counting sort is particularly efficient when the range of input values is small compared to the number of elements to be sorted.
This implementation makes the following assumptions:
values
contains all distinct values present inself
.freq
andvalues
are of the same length.freq
only contains zeros.
Returns None
if values
does not contain a value present in self
,
or if self
has more elements than freq
can accommodate.
Note that the frequencies in freq
will be in the order of the sorted
distinct elements in values
.
§Examples
let mut data = [4, 64, 4, 2, 4, 8, 8, 4, 8, 4, 2, 8, 64, 4, 8, 4, 2];
let values = [64, 4, 2, 8];
let mut freq = [0; 4];
Sort(&mut data[..]).counting_buf(&mut freq, &values);
assert_eq![data, [64, 64, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 8, 8, 8, 8, 8]];
assert_eq![freq, [2, 7, 3, 5]];
§Panics
Panics in debug if the length of freq
and values
is not the same.
Sourcepub fn insertion(self)
pub fn insertion(self)
Sorts a slice using insertion sort.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort(&mut arr[..]).insertion();
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn merge(self)where
T: Copy,
Available on crate feature alloc
only.
pub fn merge(self)where
T: Copy,
alloc
only.Sorts a slice
using merge sort.
It allocates one vector for the entire sort operation.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort(&mut arr[..]).merge();
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Source§impl<'a, T: Ord + 'a> Sort<&'a mut [T]>
impl<'a, T: Ord + 'a> Sort<&'a mut [T]>
Sourcepub fn quick_lomuto(slice: &mut [T])
pub fn quick_lomuto(slice: &mut [T])
Sorts a slice
using quick sort with the Lomuto partition scheme.
It performs more swaps compared to the Hoare partition scheme.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
// Sort(&mut arr[..]).quick_lomuto();
Sort::quick_lomuto(&mut arr[..]);
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn quick_3way(slice: &mut [T])where
T: Clone,
pub fn quick_3way(slice: &mut [T])where
T: Clone,
Sorts a slice
using quick sort with the Three way partition scheme.
It is more efficient when dealing with duplicate elements.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort::quick_3way(&mut arr);
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Sourcepub fn quick_hoare(slice: &mut [T])where
T: Clone,
pub fn quick_hoare(slice: &mut [T])where
T: Clone,
Sorts a slice
using quick sort with the Hoare partition scheme.
It performs fewer swaps compared to the Lomuto partition scheme.
§Examples
let mut arr = [4, 7, -5, 1, -13, 0];
Sort::quick_hoare(&mut arr);
assert_eq![arr, [-13, -5, 0, 1, 4, 7]];
Auto Trait Implementations§
impl<T> Freeze for Sort<T>where
T: Freeze,
impl<T> RefUnwindSafe for Sort<T>where
T: RefUnwindSafe,
impl<T> Send for Sort<T>where
T: Send,
impl<T> Sync for Sort<T>where
T: Sync,
impl<T> Unpin for Sort<T>where
T: Unpin,
impl<T> UnwindSafe for Sort<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId
of Self
using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more