Function sortrs::introsort_by [] [src]

pub fn introsort_by<T: PartialOrd, F>(v: &mut [T], lt: F) where
    F: Fn(&T, &T) -> bool

Sorts the slice, in place, using lt to compare elements.

The order of equal elements is not guaranteed to be preserved.

This sort is O(n log n) worst-case and stable.

The sort is implemented using the Introsort algorithm. Introsort or introspective sort is a hybrid sorting algorithm that provides both fast average performance and (asymptotically) optimal worst-case performance. It begins with quicksort and switches to heapsort when the recursion depth exceeds a level based on (the logarithm of) the number of elements being sorted. This combines the good parts of both algorithms, with practical performance comparable to quicksort on typical data sets and worst-case O(n log n) runtime due to the heap sort.

Examples

let mut v = [5, 4, 1, 3, 2];
sortrs::introsort_by(&mut v, |a, b| a.lt(b));
assert!(v == [1, 2, 3, 4, 5]);

// reverse sorting
sortrs::introsort_by(&mut v, |a, b| b.lt(a));
assert!(v == [5, 4, 3, 2, 1]);