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(Integer[] values)
median
if (values == null || values.length <= 0) {
    return null;
Arrays.sort(values);
int middle = values.length / 2;
if (values.length % 2 == 0) {
    return ((double) (values[middle] + values[middle - 1])) / 2.0;
} else {
...
doublemedian(long[] array)
median
return median(array, array.length);
doublemedian(Number[] array)
Returns the median of the given array.
double result;
Number[] sorted;
if (array.length == 0)
    return Double.NaN;
sorted = array.clone();
Arrays.sort(sorted);
if (sorted.length % 2 == 0)
    result = (sorted[sorted.length / 2 - 1].doubleValue() + sorted[sorted.length / 2].doubleValue()) / 2;
...
doublemedian(short[] arr)
Returns the median value of a short array.
Arrays.sort(arr);
int n = arr.length;
int mid = n / 2;
double median = arr[mid];
if ((n & 0x1) == 0) {
    median = (median + arr[mid - 1]) / 2;
return median;
...
doublemedian3(double[] v)
median
sortInPlace(v, 0, 1);
sortInPlace(v, 1, 2);
sortInPlace(v, 0, 1);
return v[1];
doublemedian7(double[] v)
median
sortInPlace(v, 0, 5);
sortInPlace(v, 0, 3);
sortInPlace(v, 1, 6);
sortInPlace(v, 2, 4);
sortInPlace(v, 0, 1);
sortInPlace(v, 3, 5);
sortInPlace(v, 2, 6);
sortInPlace(v, 2, 3);
...
intmedian_of_3(int[] x, int x_ptr)
mediao_
int t0, t1, t2;
if (x[x_ptr] > x[x_ptr + 1]) {
    t0 = x[x_ptr + 1];
    t1 = x[x_ptr];
} else {
    t0 = x[x_ptr];
    t1 = x[x_ptr + 1];
t2 = x[x_ptr + 2];
if (t1 < t2) {
    return t1;
} else if (t0 < t2) {
    return t2;
} else {
    return t0;
doublemedian_sorted(double[] sorted)
mediasorted
if (sorted.length % 2 == 1)
    return sorted[(sorted.length + 1) / 2 - 1];
else {
    double lower = sorted[sorted.length / 2 - 1];
    double upper = sorted[sorted.length / 2];
    return (lower + upper) * 0.5;
doublemedianAndSort(double[] a)
Returns the median value of input array and sorts array in place.
if (a != null) {
    int n = a.length;
    if (n > 1) {
        int halfN = n / 2;
        Arrays.sort(a);
        if (n % 2 == 0) {
            return (0.5 * (a[halfN] + a[halfN - 1]));
        } else {
...
floatmedianElement(float[] array, int size)
Calculate the median value of a float array.
float[] tmpArray = new float[size];
System.arraycopy(array, 0, tmpArray, 0, size);
boolean changed = true;
while (changed) {
    changed = false;
    for (int i = 0; i < size - 1; i++) {
        if (tmpArray[i] > tmpArray[i + 1]) {
            changed = true;
...