Java Utililty Methods Median

List of utility methods to do Median

Description

The list of methods to do Median are organized into topic(s).

Method

doublemedian(final double... values)
Calculates and returns the median of an array of double.
if ((values != null) && (values.length != 0)) {
    Arrays.sort(values);
    final int medianIndex = (int) Math.floor(values.length / 2);
    double median = values[medianIndex - 1];
    if ((values.length % 2) == 0) {
        median += values[medianIndex];
        median = median / 2;
    return median;
} else {
    throw new IllegalArgumentException("cannot calculate median of null or empty array of values");
doublemedian(final double[] values)
Computes the median of the values in the input array.
final int len = values.length;
final double[] sortedValues = Arrays.copyOf(values, len);
Arrays.sort(sortedValues);
if (len % 2 == 0) {
    return ((double) sortedValues[len / 2] + (double) sortedValues[len / 2 - 1]) / 2;
} else {
    return (double) sortedValues[len / 2];
doublemedian(final int[] values)
Get the median of an array of integer.
return percentile(values, 50.0);
floatmedian(float a, float b, float c)
median
return (a <= b) ? ((b <= c) ? b : ((a < c) ? c : a)) : ((a <= c) ? a : ((b < c) ? c : b));
floatmedian(float[] vals)
median
final float[] newVals = Arrays.copyOf(vals, vals.length);
Arrays.sort(newVals);
return newVals[newVals.length / 2];
floatmedian(float[] values)
Calculates the median for the list of numbers.
if (values.length == 1) {
    return values[0];
Arrays.sort(values);
int middle = values.length / 2;
if (values.length % 2 == 1) {
    return values[middle];
} else {
...
floatmedian(float[] vector)
Calculates the median for a given vector (float array)
float median = 0;
float[] temp = new float[vector.length];
for (int i = 0; i < temp.length; i++) {
    if (Float.isNaN(vector[i]))
        temp[i] = 0;
    else
        temp[i] = vector[i];
Arrays.sort(temp);
if ((temp.length % 2) == 0)
    median = (temp[(int) Math.floor(temp.length / 2)] + temp[(int) Math.floor((temp.length + 1) / 2)]) / 2;
else
    median = temp[(int) Math.floor((temp.length + 1) / 2)];
return median;
intmedian(int x[], int pos1, int pos2, int pos3)
median
int v1 = x[pos1];
int v2 = x[pos2];
int v3 = x[pos3];
if (v1 < v2) {
    if (v2 <= v3) {
        return pos2;
    } else {
        return v1 < v3 ? pos3 : pos1;
...
doublemedian(int[] m)
median
int[] sorted = Arrays.copyOf(m, m.length);
Arrays.sort(sorted);
int middle = sorted.length / 2; 
if (sorted.length % 2 == 1) {
    return sorted[middle];
} else {
    return (sorted[middle - 1] + sorted[middle]) / 2.0;
doublemedian(int[] vals)
median
int[] copy = Arrays.copyOf(vals, vals.length);
Arrays.sort(copy);
if (copy.length % 2 == 0) {
    int acc = copy[copy.length / 2];
    acc += copy[(copy.length / 2) - 1];
    return acc / 2.0;
} else {
    return copy[copy.length / 2];
...