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(double[] unsorted)
median
double[] sorted = Arrays.copyOf(unsorted, unsorted.length);
Arrays.sort(sorted);
return median_sorted(sorted);
doublemedian(double[] v)
Computes the median of an array.
if (v.length == 3) {
    return median3(copy(v));
} else if (v.length == 5) {
    return median5(copy(v));
} else if (v.length == 7) {
    return median7(copy(v));
} else if (v.length % 2 == 1) { 
    return quickSelect(copy(v), false);
...
doublemedian(double[] vals)
|vals| is const.
if (vals.length == 0) {
    return Double.NaN;
double[] copy = Arrays.copyOf(vals, vals.length);
return nonConstMedian(copy);
doublemedian(double[] values)
Given an array of values, compute the median of these values.
double[] a = (double[]) values.clone();
Arrays.sort(a);
int len = a.length;
if (len % 2 == 0) {
    return (a[len / 2 - 1] + a[len / 2]) / 2;
} else {
    return a[(len - 1) / 2];
doublemedian(double[] x)
median
if (x != null && x.length > 0)
    return median(x, 0, x.length - 1);
else
    return 0.0;
doublemedian(double[] x)
median
quickSort(x);
int index = (int) Math.floor(0.5 * x.length + 0.5);
if (index < 0)
    index = 0;
if (index > x.length - 1)
    index = x.length - 1;
return x[index];
doublemedian(double[] x)
Returns the median of this array
if (x.length == 1) {
    return x[0];
double[] y = new double[x.length];
System.arraycopy(x, 0, y, 0, x.length);
Arrays.sort(y);
int n = x.length;
double m = ((double) n) / 2.0;
...
doublemedian(double[][] values)
median
double[] flatvalues = new double[values.length * values.length];
for (int i = 0; i < values.length; i++) {
    for (int j = 0; j < values.length; j++) {
        flatvalues[i * values.length + j] = values[i][j];
if (flatvalues.length % 2 == 0)
    return flatvalues[flatvalues.length / 2];
...
longmedian(final ArrayList values)
Compute median of a sorted list of Long s.
if (values.size() % 2 == 1)
    return values.get((values.size() + 1) / 2 - 1);
final long lower = values.get(values.size() / 2 - 1);
final long upper = values.get(values.size() / 2);
return (lower + upper) / 2;
doublemedian(final double... in)
Calculate the median of an array of doubles.
if (in.length == 0) {
    throw new IllegalArgumentException("Attempting to find the median of an empty array");
final double[] data = Arrays.copyOf(in, in.length);
Arrays.sort(data);
final int middle = data.length / 2;
return data.length % 2 == 1 ? data[middle] : (data[middle - 1] + data[middle]) / 2.0;