Java Median median(Number[] array)

Here you can find the source of median(Number[] array)

Description

Returns the median of the given array.

License

Open Source License

Parameter

Parameter Description
array the array to work on

Return

the median

Declaration

public static double median(Number[] array) 

Method Source Code


//package com.java2s;
/*//from www  .  ja v a 2 s.  c  o  m
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Returns the median of the given array.
     * NaN is returned in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the median
     */
    public static double median(Number[] array) {
        double result;
        Number[] sorted;

        if (array.length == 0)
            return Double.NaN;

        sorted = array.clone();
        Arrays.sort(sorted);

        if (sorted.length % 2 == 0)
            result = (sorted[sorted.length / 2 - 1].doubleValue() + sorted[sorted.length / 2].doubleValue()) / 2;
        else
            result = sorted[sorted.length / 2].doubleValue();

        return result;
    }

    /**
     * Returns the median of the given array.
     * NaN is returned in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the median
     */
    public static double median(int[] array) {
        return median(toNumberArray(array));
    }

    /**
     * Returns the median of the given array.
     * NaN is returned in case of zero-length arrays.
     *
     * @param array   the array to work on
     * @return      the median
     */
    public static double median(double[] array) {
        return median(toNumberArray(array));
    }

    /**
     * Returns a sorted copy of the array (ascending).
     *
     * @param array   the array to sort
     * @return      the sorted array
     */
    public static Number[] sort(Number[] array) {
        return sort(array, true);
    }

    /**
     * Returns a sorted copy of the array (ascending).
     *
     * @param array   the array to sort
     * @return      the sorted array
     */
    public static int[] sort(int[] array) {
        return sort(array, true);
    }

    /**
     * Returns a sorted copy of the array (ascending).
     *
     * @param array   the array to sort
     * @return      the sorted array
     */
    public static double[] sort(double[] array) {
        return sort(array, true);
    }

    /**
     * Returns a sorted copy of the array (ascending or descending).
     *
     * @param array   the array to sort
     * @param asc      if true then the data gets sorted in ascending manner,
     *          otherwise in descending manner
     * @return      the sorted array
     */
    public static Number[] sort(Number[] array, boolean asc) {
        Number[] sorted;
        Number value;
        int i;
        int n;

        sorted = array.clone();
        Arrays.sort(sorted);

        if (!asc) {
            for (i = 0; i < array.length / 2; i++) {
                n = sorted.length - i - 1;
                value = sorted[i];
                sorted[i] = sorted[n];
                sorted[n] = value;
            }
        }

        return sorted;
    }

    /**
     * Returns a sorted copy of the array (ascending or descending).
     *
     * @param array   the array to sort
     * @param asc      if true then the data gets sorted in ascending manner,
     *          otherwise in descending manner
     * @return      the sorted array
     */
    public static int[] sort(int[] array, boolean asc) {
        int[] result;
        Integer[] sorted;
        int i;

        sorted = (Integer[]) sort(toNumberArray(array), asc);
        result = new int[sorted.length];
        for (i = 0; i < sorted.length; i++)
            result[i] = sorted[i];

        return result;
    }

    /**
     * Returns a sorted copy of the array (ascending or descending).
     *
     * @param array   the array to sort
     * @param asc      if true then the data gets sorted in ascending manner,
     *          otherwise in descending manner
     * @return      the sorted array
     */
    public static double[] sort(double[] array, boolean asc) {
        double[] result;
        Double[] sorted;
        int i;

        sorted = (Double[]) sort(toNumberArray(array), asc);
        result = new double[sorted.length];
        for (i = 0; i < sorted.length; i++)
            result[i] = sorted[i];

        return result;
    }

    /**
     * Turns the byte array into a Byte array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(byte[] array) {
        Byte[] result;
        int i;

        result = new Byte[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Byte(array[i]);

        return result;
    }

    /**
     * Turns the short array into a Short array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(short[] array) {
        Short[] result;
        int i;

        result = new Short[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Short(array[i]);

        return result;
    }

    /**
     * Turns the int array into a Integer array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(int[] array) {
        Integer[] result;
        int i;

        result = new Integer[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Integer(array[i]);

        return result;
    }

    /**
     * Turns the long array into a Long array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(long[] array) {
        Long[] result;
        int i;

        result = new Long[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Long(array[i]);

        return result;
    }

    /**
     * Turns the float array into a Float array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(float[] array) {
        Float[] result;
        int i;

        result = new Float[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Float(array[i]);

        return result;
    }

    /**
     * Turns the double array into a Double array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(double[] array) {
        Double[] result;
        int i;

        result = new Double[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Double(array[i]);

        return result;
    }
}

Related

  1. median(int x[], int pos1, int pos2, int pos3)
  2. median(int[] m)
  3. median(int[] vals)
  4. median(Integer[] values)
  5. median(long[] array)
  6. median(short[] arr)
  7. median3(double[] v)
  8. median7(double[] v)
  9. median_of_3(int[] x, int x_ptr)