Java Median median(short[] arr)

Here you can find the source of median(short[] arr)

Description

Returns the median value of a short array.

License

Open Source License

Parameter

Parameter Description
arr a short array

Return

the median value of the array as a double

Declaration

public static double median(short[] arr) 

Method Source Code


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

import java.util.Arrays;

public class Main {
    /**//  w  w w.  ja va  2s. c om
     * Returns the median value of a short array. The method sorts the input
     * array. If the array has odd length the median is the central value 
     * arr[arr.length/2]. If the array has even length the median is the average
     * of the two central values arr[(arr.length/2)-1]+arr[arr.length/2])/2. 
     *
     * @param   arr a short array
     * @return  the median value of the array as a double
     */
    public static double median(short[] arr) {
        Arrays.sort(arr);
        int n = arr.length;
        int mid = n / 2;
        double median = arr[mid];
        if ((n & 0x1) == 0) {
            median = (median + arr[mid - 1]) / 2;
        }
        return median;
    }

    /**
     * Returns the median value of an integer array. The method sorts the input
     * array. If the array has odd length the median is the central value 
     * arr[arr.length/2]. If the array has even length the median is the average
     * of the two central values arr[(arr.length/2)-1]+arr[arr.length/2])/2. 
     *
     * @param   arr an integer array
     * @return  the median value of the array as a double
     */
    public static double median(int[] arr) {
        Arrays.sort(arr);
        int n = arr.length;
        int mid = n / 2;
        double median = arr[mid];
        if ((n & 0x1) == 0) {
            median = (median + arr[mid - 1]) / 2;
        }
        return median;
    }

    /**
     * Returns the median value of a long array. The method sorts the input
     * array. If the array has odd length the median is the central value 
     * arr[arr.length/2]. If the array has even length the median is the average
     * of the two central values arr[(arr.length/2)-1]+arr[arr.length/2])/2. 
     *
     * @param   arr a long array
     * @return  the median value of the array as a double
     */
    public static double median(long[] arr) {
        Arrays.sort(arr);
        int n = arr.length;
        int mid = n / 2;
        double median = arr[mid];
        if ((n & 0x1) == 0) {
            median = (median + arr[mid - 1]) / 2;
        }
        return median;
    }

    /**
     * Returns the median value of a float array. The method sorts the input
     * array. If the array has odd length the median is the central value 
     * arr[arr.length/2]. If the array has even length the median is the average
     * of the two central values arr[(arr.length/2)-1]+arr[arr.length/2])/2. 
     *
     * @param   arr a float array
     * @return  the median value of the array as a double
     */
    public static double median(float[] arr) {
        Arrays.sort(arr);
        int n = arr.length;
        int mid = n / 2;
        double median = arr[mid];
        if ((n & 0x1) == 0) {
            median = (median + arr[mid - 1]) / 2;
        }
        return median;
    }

    /**
     * Returns the median value of a double array. The method sorts the input
     * array. If the array has odd length the median is the central value 
     * arr[arr.length/2]. If the array has even length the median is the average
     * of the two central values arr[(arr.length/2)-1]+arr[arr.length/2])/2. 
     *
     * @param   arr  a double array
     * @return  the median value of the array as a double
     */
    public static double median(double[] arr) {
        Arrays.sort(arr);
        int n = arr.length;
        int mid = n / 2;
        double median = arr[mid];
        if ((n & 0x1) == 0) {
            median = (median + arr[mid - 1]) / 2;
        }
        return median;
    }
}

Related

  1. median(int[] m)
  2. median(int[] vals)
  3. median(Integer[] values)
  4. median(long[] array)
  5. median(Number[] array)
  6. median3(double[] v)
  7. median7(double[] v)
  8. median_of_3(int[] x, int x_ptr)
  9. median_sorted(double[] sorted)