Java Median getMedian(double[] a)

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

Description

Returns the median of an array.

License

Apache License

Parameter

Parameter Description
a the array.

Return

the median of an array.

Declaration

public static double getMedian(double[] a) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    /**// w  ww.  j ava 2 s  . com
     * Returns the median of an array.
     * 
     * @param a the array.
     * @return the median of an array.
     */
    public static double getMedian(double[] a) {
        if (a.length == 0) {
            return 0;
        }
        double[] ary = Arrays.copyOf(a, a.length);
        Arrays.sort(ary);
        int mid = ary.length / 2;
        if (ary.length % 2 == 1) {
            return ary[mid];
        } else {
            return (ary[mid - 1] + ary[mid]) / 2;
        }
    }
}

Related

  1. calcMedian(final double[] values)
  2. calculateMedianOfArrayListInteger(List integerList)
  3. getMedian(double array[])
  4. getmedian(double[] vals, int nvalindex)
  5. getMedian(int[] array)
  6. getMedianValue(ArrayList values)
  7. median(ArrayList v)