Function order_stat::median_of_medians_by [] [src]

pub fn median_of_medians_by<T, F>(array: &mut [T], cmp: F) -> (usize, &mut T) where F: FnMut(&T, &T) -> Ordering

Calculate an approximate median of array, using the ordering defined by cmp.

The return value is the index/reference to some value of array that is guaranteed to lie between the 30th and 70th percentiles of the values in array. That is, the return value is such that cmp will return Greater for at most 70% of the elements and similarly will return Less for at most 70%.

Panics

This panics if array is empty.

Examples

// the numbers 0.0, 1.0, ..., 100.0.
let mut v = (0..101).map(|x| x as f64).rev().collect::<Vec<_>>();

let (_, &mut median) = order_stat::median_of_medians_by(&mut v, |x, y| x.partial_cmp(y).unwrap());
assert!(30.0 <= median);
assert!(median <= 70.0);