let median a = let n = Array.length a in assert (n > 0); let a = Array.copy a in Array.sort ~cmp:Pervasives.compare a; if odd n then a.((n+1)/2 - 1) else let m = (n+1)/2 in (a.(m-1) +. a.(m)) /. 2.0