Java Median median(double[] a)

Here you can find the source of median(double[] a)

Description

Computes the median value of an array.

License

Apache License

Parameter

Parameter Description
a input array

Return

median value in the specified range of input array; if array is zero length, method returns NaN

Declaration

public static final double median(double[] a) 

Method Source Code

//package com.java2s;
/*//from w  w w .  ja  v a 2  s  . co m
 * Copyright 2014 Jon N. Marsh.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * Computes the median value of an array. If the array length is even, the
     * value returned is equal to the average of the middle two values of the
     * sorted array. The input array is left unchanged. {@code null} input
     * returns {@code Double.NaN}.
     *
     * @param a   input array
     * @return median value in the specified range of input array; if array is
     *         zero length, method returns {@code NaN}
     */
    public static final double median(double[] a) {
        if (a != null) {
            return median(a, 0, a.length);
        } else {
            return Double.NaN;
        }
    }

    /**
     * Computes the median value in the specified range of an array. If the
     * array length is even, the value returned is equal to the average of the
     * middle two values of the sorted array. No error checking is performed on
     * range limits; if the values are negative or outside the range of the
     * array, unexpected results may occur or a runtime exception may be thrown.
     * The input array is left unchanged.
     *
     * @param a    input array
     * @param from initial index of the range to compute the median, inclusive
     * @param to   final index of the range to compute the median, exclusive
     * @return median value in the specified range of input array; if array
     *         range is zero, method returns {@code NaN}
     */
    public static final double median(double[] a, int from, int to) {
        int n = to - from;
        if (n > 1) {
            int halfN = n / 2;
            final double[] temp = new double[n];
            System.arraycopy(a, from, temp, 0, n);
            Arrays.sort(temp);
            if (n % 2 == 0) {
                return (0.5 * (temp[halfN] + temp[halfN - 1]));
            } else {
                return temp[halfN];
            }
        } else if (n == 1) {
            return a[from];
        } else {
            return Double.NaN;
        }
    }

    /**
     * Computes the median value of an array. If the array length is even, the
     * value returned is equal to the average of the middle two values of the
     * sorted array. The input array is left unchanged. {@code null} input
     * returns {@code Double.NaN}.
     *
     * @param a   input array
     * @return median value in the specified range of input array; if array is
     *         zero length, method returns {@code NaN}
     */
    public static final float median(float[] a) {
        if (a != null) {
            return median(a, 0, a.length);
        } else {
            return Float.NaN;
        }
    }

    /**
     * Computes the median value in the specified range of an array. If the
     * array length is even, the value returned is equal to the average of the
     * middle two values of the sorted array. No error checking is performed on
     * range limits; if the values are negative or outside the range of the
     * array, unexpected results may occur or a runtime exception may be thrown.
     * The input array is left unchanged. {@code null} input returns
     * {@code Double.NaN}.
     *
     * @param a    input array
     * @param from initial index of the range to compute the median, inclusive
     * @param to   final index of the range to compute the median, exclusive
     * @return median value in the specified range of input array; if array
     *         range is zero, method returns {@code NaN}
     */
    public static final float median(float[] a, int from, int to) {
        int n = to - from;
        if (n > 1) {
            int halfN = n / 2;
            final float[] temp = new float[n];
            System.arraycopy(a, from, temp, 0, n);
            Arrays.sort(temp);
            if (n % 2 == 0) {
                return (0.5f * (temp[halfN] + temp[halfN - 1]));
            } else {
                return temp[halfN];
            }
        } else if (n == 1) {
            return a[from];
        } else {
            return Float.NaN;
        }
    }
}

Related

  1. getMedianValue(ArrayList values)
  2. median(ArrayList v)
  3. median(ArrayList values)
  4. Median(ArrayList values)
  5. median(double... a)
  6. median(double[] a)
  7. median(double[] arr)
  8. median(double[] array)
  9. median(double[] array)