Returns median - Java java.lang

Java examples for java.lang:Math Function

Description

Returns median

Demo Code


//package com.java2s;
import java.util.Arrays;

public class Main {
    /**// ww w .  java 2s  .c o m
     *
     * Returns median
     * 
     * @param data
     * @return median
     */
    public static double getMedian(double[] data) {
        Arrays.sort(data);
        if (data.length % 2 == 0) {
            return (data[data.length / 2] + data[data.length / 2 - 1]) / 2;
        } else {
            return data[(data.length - 1) / 2];
        }
    }
}

Related Tutorials