pub fn sort<T: Sortable>(list: &mut [T])Expand description
Cocktail sorts in-place, stable, in ascending order a mutable ref slice of type T: Sortable
Cocktail is a variant of bubble sort. It continuously loops over elements in slice collection, swapping elements if they are out of order. If no swaps occur in a loop then the sort is complete. Each iteration swaps order of the iteration from left to right and from comparing smallest to largest, such that the largest and smallest elements are bubbled up and down the list bidirectionally. It aims to solve the rabbit and turtle problem of standard bubble sort where values that need to move to the beginning of the list require many swaps to get there.
ยงExamples
use rust_sort::cocktail_sort::sort;
let mut arr = [3, 2, 1, 7, 9, 4, 1, 2];
sort(&mut arr);
assert_eq!(arr, [1, 1, 2, 2, 3, 4, 7, 9]);