Formula for median of a set (sorted)

AI Thread Summary
The discussion focuses on deriving a formula for calculating the median of a sorted set of numerical observations. The initial formula presented is ##n-1/2=x##, leading to ##x+1=Median##, but it is acknowledged as a simplified version. The correct approach involves checking if the array size is even or odd; if even, the median is the average of the two middle values, and if odd, it is the middle value. A sample code snippet illustrates this logic, emphasizing the importance of handling both scenarios. The conversation highlights the need for clarity in deriving and implementing the median calculation.
shadowboy13
Messages
20
Reaction score
0
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
 
Physics news on Phys.org
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 ];
}
 
Back
Top