Struct InsertionSort

Source
pub struct InsertionSort { /* private fields */ }
Expand description

InsertionSort implements the Sorter trait with the Insertion Sort algorithm. A brief explanation of the sort:

The algorithm divides the slice into two parts: an already sorted section, at the beginning, and a potentially unsorted section at the end.

// v already sorted array, containing only the number 5
[  5, 1, 3, 2, 7];
//    ^ Start of unsorted array, containing numbers 1, 3, 2, and 7. Also the next item that will
//      be considered for adding to the sorted section, read on below

Assuming our goal is an ascending sort (lowest item at the start of the array, highest item at the end of the array), each step of the algorithm does the following:

The first item of the unsorted section is considered for the sorted section. It is then compared with the last item of the sorted section.

If the new item is higher than the previous last in the sorted section, we’re done; as the sorted section is already, well, sorted, we can be sure that adding the new item keeps the ordering of the already sorted section.

If the new item is lower than the previous last in the sorted section, they are swapped. The algorithm then continues and compares the item with the next one in the unsorted section. This is repeated until an item is encountered that is smaller than the one we’re trying to place.

To visualize, continuing on with the previous example:

// v already sorted
[  5, 1, 3, 2, 7];
//    ^ next item
//      will be swapped with 5

// v--v already sorted.
[  1, 5, 3, 2, 7];
//       ^ next item
//         will be swapped with 5

// v-----v already sorted
[  1, 3, 5, 2, 7];
//          ^ next item.
//            will be swapped with 5, then with 3

// v--------v already sorted
[  1, 2, 3, 5, 7];
//             ^ next item
//               no swap will take place.

// v-----------v already sorted
[  1, 2, 3, 5, 7];
// no next item.

Implementations§

Source§

impl InsertionSort

Source

pub fn asc() -> Self

Creates an InsertionSort struct which will sort the items in ascending order.

Source

pub fn desc() -> Self

Creates an InsertionSort struct which will sort the items in descending order.

Trait Implementations§

Source§

impl<T> Sorter<T> for InsertionSort
where T: Ord,

Source§

fn sort(&self, items: &mut [T])

Auto Trait Implementations§

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.