Here you can find the source of median(short[] arr)
Parameter | Description |
---|---|
arr | a short array |
public static double median(short[] arr)
//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; } }