%mathpiper,def="Median"

Median(list) :=
[
    Check(IsList(list), "Argument", "Argument must be a list.");
    
    Local(sx,n,n2); // s[orted]x
    
    sx := HeapSort(list,"<");
    
    n := Length(list);
    
    n2 := (n>>1);
    
    If(Modulo(n,2) = 1, sx[n2+1], (sx[n2]+sx[n2+1])/2);
];

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output



%mathpiper_docs,name="Median",categories="User Functions;Statistics & Probability"
*CMD Median --- calculates the median of a list of values
*STD
*CALL
	Median(list)

*PARMS

{list} -- list of values

*DESC

This function calculates the median of a list of values.

*E.G.
In> Median({73,94,80,37,57,94,40,21,7,26})
Result: 97/2

In> N(Median({73,94,80,37,57,94,40,21,7,26}))
Result: 48.5

*SEE WeightedMean, Mean, Mode, GeometricMean
%/mathpiper_docs

