Java Median median(float[] values)

Here you can find the source of median(float[] values)

Description

Calculates the median for the list of numbers.

License

BSD License

Parameter

Parameter Description
values of numbers to calculate median for.

Return

median value.

Declaration

public static float median(float[] values) 

Method Source Code

//package com.java2s;
/**/*w  w w  .j  a  v a 2s.c o  m*/
 * Copyright 5AM Solutions Inc, ESAC, ScenPro & SAIC
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caintegrator/LICENSE.txt for details.
 */

import java.util.Arrays;

public class Main {
    /**
     * Calculates the median for the list of numbers.
     *
     * @param values of numbers to calculate median for.
     * @return median value.
     */
    public static float median(float[] values) {
        if (values.length == 1) {
            return values[0];
        }
        Arrays.sort(values);
        int middle = values.length / 2;
        if (values.length % 2 == 1) {
            return values[middle];
        } else {
            return (values[middle - 1] + values[middle]) / 2.0f;
        }
    }
}

Related

  1. median(final double... values)
  2. median(final double[] values)
  3. median(final int[] values)
  4. median(float a, float b, float c)
  5. median(float[] vals)
  6. median(float[] vector)
  7. median(int x[], int pos1, int pos2, int pos3)
  8. median(int[] m)
  9. median(int[] vals)