Here you can find the source of median(double[] x)
public static double median(double[] x)
//package com.java2s; /**/*w w w.j a va 2s. c o m*/ * Copyright 2004-2006 DFKI GmbH. * All Rights Reserved. Use is subject to license terms. * * This file is part of MARY TTS. * * MARY TTS is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ public class Main { public static double median(double[] x) { if (x != null && x.length > 0) return median(x, 0, x.length - 1); else return 0.0; } public static double median(double[] xin, int firstIndex, int lastIndex) { if (firstIndex < 0) firstIndex = 0; if (lastIndex > xin.length - 1) lastIndex = xin.length - 1; if (lastIndex < firstIndex) lastIndex = firstIndex; double[] x = new double[lastIndex - firstIndex + 1]; System.arraycopy(xin, firstIndex, x, 0, x.length); quickSort(x); int index = (int) Math.floor(0.5 * x.length + 0.5); if (index < 0) index = 0; if (index > x.length - 1) index = x.length - 1; return x[index]; } public static int[] quickSort(int[] x) { double[] x2 = new double[x.length]; int i; for (i = 0; i < x.length; i++) x2[i] = x[i]; int[] inds = quickSort(x2); for (i = 0; i < x.length; i++) x[i] = (int) x2[i]; return inds; } public static int[] quickSort(double[] x) { int[] indices = new int[x.length]; for (int i = 0; i < x.length; i++) indices[i] = i; quickSort(x, indices); return indices; } public static int[] quickSort(float[] x) { int[] indices = new int[x.length]; for (int i = 0; i < x.length; i++) indices[i] = i; quickSort(x, indices); return indices; } public static int[] quickSort(double[] x, int startIndex, int endIndex) { if (startIndex < 0) startIndex = 0; if (startIndex > x.length - 1) startIndex = x.length - 1; if (endIndex < startIndex) endIndex = startIndex; if (endIndex > x.length - 1) endIndex = x.length - 1; int[] indices = new int[endIndex - startIndex + 1]; double[] x2 = new double[endIndex - startIndex + 1]; int i; for (i = startIndex; i <= endIndex; i++) { indices[i - startIndex] = i; x2[i - startIndex] = x[i]; } quickSort(x2, indices); for (i = startIndex; i <= endIndex; i++) x[i] = x2[i - startIndex]; return indices; } public static int[] quickSort(float[] x, int startIndex, int endIndex) { if (startIndex < 0) startIndex = 0; if (startIndex > x.length - 1) startIndex = x.length - 1; if (endIndex < startIndex) endIndex = startIndex; if (endIndex > x.length - 1) endIndex = x.length - 1; int[] indices = new int[endIndex - startIndex + 1]; float[] x2 = new float[endIndex - startIndex + 1]; int i; for (i = startIndex; i <= endIndex; i++) { indices[i - startIndex] = i; x2[i - startIndex] = x[i]; } quickSort(x2, indices); for (i = startIndex; i <= endIndex; i++) x[i] = x2[i - startIndex]; return indices; } public static void quickSort(double[] x, int[] y) { assert x.length == y.length; quickSort(x, y, 0, x.length - 1); } public static void quickSort(float[] x, int[] y) { assert x.length == y.length; quickSort(x, y, 0, x.length - 1); } public static void quickSort(double[] x, int[] y, int startIndex, int endIndex) { if (startIndex < endIndex) { int j = partition(x, y, startIndex, endIndex); quickSort(x, y, startIndex, j - 1); quickSort(x, y, j + 1, endIndex); } } public static void quickSort(float[] x, int[] y, int startIndex, int endIndex) { if (startIndex < endIndex) { int j = partition(x, y, startIndex, endIndex); quickSort(x, y, startIndex, j - 1); quickSort(x, y, j + 1, endIndex); } } private static int partition(double[] x, int[] y, int startIndex, int endIndex) { int i = startIndex; int j = endIndex + 1; double t; int ty; double pivot = x[startIndex]; while (true) { do { ++i; } while (i <= endIndex && x[i] <= pivot); do { --j; } while (x[j] > pivot); if (i >= j) break; t = x[i]; ty = y[i]; x[i] = x[j]; y[i] = y[j]; x[j] = t; y[j] = ty; } t = x[startIndex]; ty = y[startIndex]; x[startIndex] = x[j]; y[startIndex] = y[j]; x[j] = t; y[j] = ty; return j; } private static int partition(float[] x, int[] y, int startIndex, int endIndex) { int i = startIndex; int j = endIndex + 1; float t; int ty; float pivot = x[startIndex]; while (true) { do { ++i; } while (i <= endIndex && x[i] <= pivot); do { --j; } while (x[j] > pivot); if (i >= j) break; t = x[i]; ty = y[i]; x[i] = x[j]; y[i] = y[j]; x[j] = t; y[j] = ty; } t = x[startIndex]; ty = y[startIndex]; x[startIndex] = x[j]; y[startIndex] = y[j]; x[j] = t; y[j] = ty; return j; } }