/// Median of a &[T] slice by sorting
/// Works slowly but gives exact results
/// Sorts its mutable slice argument as a side effect
/// # Example
/// ```
/// use medians::naive_median;
/// let mut v = vec![1_u8,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
/// let res = naive_median(&mut v);
/// assert_eq!(res,8_f64);
/// ```
Median of a &[T] slice by sorting
Works slowly but gives exact results
Sorts its mutable slice argument as a side effect
Median of a & [ T ] slice by sorting Works slowly but gives exact results Sorts its mutable slice argument as a side effect
pub fn naive_median<T>(s:&mut [T]) -> f64
    where T: Copy+PartialOrd,f64:From<T> {
    let n = s.len();
    if n == 0 { panic!("{} empty vector!",here!()); };
    if n == 1 { return f64::from(s[0]); };
    if n == 2 { return (f64::from(s[0])+f64::from(s[1]))/2.0; }; 
    s.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap()); // fastest Rust sort
    let mid = s.len()/2; // midpoint (floors odd sizes)
    if (n & 1) == 0 { (f64::from(s[mid-1]) + f64::from(s[mid])) / 2.0 } // s is even
    else { f64::from(s[mid]) } // s is odd     
}
pub fn naive_median < T > ( s : & mut [ T ] ) -> f64 where T : Copy + PartialOrd , f64 : From < T > { let n = s . len ( ) ; if n == 0 { panic ! ( " " here ( ) ) ; } ; if n == 1 { return f64 :: from ( s [ 0 ] ) ; } ; if n == 2 { return ( f64 :: from ( s [ 0 ] ) + f64 :: from ( s [ 1 ] ) ) / 2.0 ; } ; s . sort_unstable_by ( | a , b | a . partial_cmp ( b ) . unwrap ( ) ) ; // fastest Rust sort let mid = s . len ( ) / 2 ; // midpoint (floors odd sizes) if ( n & 1 ) == 0 { ( f64 :: from ( s [ mid - 1 ] ) + f64 :: from ( s [ mid ] ) ) / 2.0 } // s is even else { f64 :: from ( s [ mid ] ) } // s is odd      }
Median of a &[T] slice by sorting
Works slowly but gives exact results
Sorts its mutable slice argument as a side effect
Median of a & [ T ] slice by sorting Works slowly but gives exact results Sorts its mutable slice argument as a side effect
s
&mut [T]
example
None