Here you can find the source of getMedian(double[] a)
Parameter | Description |
---|---|
a | the array. |
public static double getMedian(double[] a)
//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; } } }