Java Median median(double[] x)

Here you can find the source of median(double[] x)

Description

median

License

Open Source License

Declaration

public static double median(double[] x) 

Method Source Code

//package com.java2s;
/**/*  w w w.j  a v a2  s  . co m*/
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */

public class Main {
    public static double median(double[] x) {
        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(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 void quickSort(double[] 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);
        }
    }

    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;
    }
}

Related

  1. median(double[] unsorted)
  2. median(double[] v)
  3. median(double[] vals)
  4. median(double[] values)
  5. median(double[] x)
  6. median(double[] x)
  7. median(double[][] values)
  8. median(final ArrayList values)
  9. median(final double... in)