Here you can find the source of median(int[] m)
public static double median(int[] m)
//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; } } }