[−][src]Function sorting_rs::cocktail_sort::cocktail_sort
pub fn cocktail_sort<T: PartialOrd>(input: &mut [T])
Sorts a slice in-place using
Cocktail sort.
All kinds of slices can be sorted as long as they implement
PartialOrd
.
Cocktail sort is a variation of bubble sort. The difference is that bubble sort only makes forward passes, whereas cocktail sort goes back and forth, like a cocktail shaker. In practice cocktail sort is faster than bubble most of the time. A small optimization can be made where the last swap position is remembered, since all elements beyond that point are already sorted.
Examples
let mut slice = vec![2,3,4,5,1]; sorting_rs::cocktail_sort(&mut slice); assert_eq!(slice, &[1,2,3,4,5]);
let mut strings = vec!["rustc", "cargo", "rustup"]; sorting_rs::cocktail_sort(&mut strings); assert_eq!(strings, &["cargo", "rustc", "rustup"]);