Java Median median(int[] vals)

Here you can find the source of median(int[] vals)

Description

median

License

Open Source License

Declaration

public static double median(int[] vals) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static double median(int[] vals) {
        int[] copy = Arrays.copyOf(vals, vals.length);
        Arrays.sort(copy);/*ww w  .j a  v  a2  s  . co  m*/

        if (copy.length % 2 == 0) {
            int acc = copy[copy.length / 2];
            acc += copy[(copy.length / 2) - 1];
            return acc / 2.0;
        } else {
            return copy[copy.length / 2];
        }
    }

    public static double median(double[] vals) {
        double[] copy = Arrays.copyOf(vals, vals.length);
        Arrays.sort(copy);

        if (copy.length % 2 == 0) {
            double acc = copy[copy.length / 2];
            acc += copy[(copy.length / 2) - 1];
            return acc / 2.0;
        } else {
            return copy[copy.length / 2];
        }

    }
}

Related

  1. median(float[] vals)
  2. median(float[] values)
  3. median(float[] vector)
  4. median(int x[], int pos1, int pos2, int pos3)
  5. median(int[] m)
  6. median(Integer[] values)
  7. median(long[] array)
  8. median(Number[] array)
  9. median(short[] arr)