# Formula for median of a set (sorted)

1. Feb 4, 2014

Now i think i derived this correctly, but i'm not sure if it's correct, can anyone give me a confirmation?

$n-1/2=x$

Where $x$ is the result of subtracting 1 from the observations and then dividing by 2.

Then $x+1=Median$

Thank you :)

Edit: Oops, this is the handicapped version of the better formula... figures

2. Feb 6, 2014

### tycoon515

If you're writing a method that returns the median of a sorted set of n numerical observations, I'm assuming they are all sorted in an array of size n. If so, you have to check whether the size of the array is an even or odd number. If even, return the arithmetic mean of the middle two observations; if odd, return the middle observation. This might look like:

public double getMedian( data [] list )
{
if( list.length % 2 == 0 )
return ( list[ list.length / 2 ] + list[ ( list.length / 2 ) - 1 ] ) / 2.0;
else
return list[ list.length / 2 ];
}