Find the Median Value of an Array (Rated 0)Description:
You can use this simple function to find the Median Value of an Array Code starts here
# median() returns a median value.
# USAGE: median(@array) where @array is
# an array of all values
# to be considered.
#
sub median {
my(@values) = sort(@_);
my($mid) = scalar(@values) >> 1;
# If there is an even number of values
# in @values, then you need to add them
# and divide by two to get the median.
return($values[$mid]) if $mid & 1;
($values[$mid-.5] + $values[$mid+.5]) / 2;
}
Submitted by Devscripts on 24-02-2003 12:06 |