Java Median median(int[] m)

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

Description

median

License

Open Source License

Declaration

public static double median(int[] m) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    public static double median(int[] m) {
        int[] sorted = Arrays.copyOf(m, m.length);
        Arrays.sort(sorted);/*from   w ww  .ja  va  2s . c o  m*/
        int middle = sorted.length / 2; // subscript of middle element
        if (sorted.length % 2 == 1) {
            // Odd number of elements -- return the middle one.
            return sorted[middle];
        } else {
            // Even number -- return average of middle two
            // Must cast the numbers to double before dividing.
            return (sorted[middle - 1] + sorted[middle]) / 2.0;
        }
    }
}

Related

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