Java List Median median(final List array)

Here you can find the source of median(final List array)

Description

Compute the median element of the list of integers

License

Open Source License

Parameter

Parameter Description
array a list of integers

Return

the median element

Declaration

public static <T extends Comparable<? super T>> T median(final List<T> array) 

Method Source Code

//package com.java2s;
/*/*from  w  ww . j  a va  2  s .c o m*/
* Copyright 2012-2016 Broad Institute, Inc.
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import java.util.*;

public class Main {
    /**
     * Compute the median element of the list of integers
     * @param array a list of integers
     * @return the median element
     */
    public static <T extends Comparable<? super T>> T median(final List<T> array) {
        /* TODO -- from Valentin
        the current implementation is not the usual median when the input is of even length. More concretely it returns the ith element of the list where i = floor(input.size() / 2).
            
        But actually that is not the "usual" definition of a median, as it is supposed to return the average of the two middle values when the sample length is an even number (i.e. median(1,2,3,4,5,6) == 3.5). [Sources: R and wikipedia]
            
        My suggestion for a solution is then:
            
        unify median and medianDoubles to public static <T extends Number> T median(Collection<T>)
        check on null elements and throw an exception if there are any or perhaps return a null; documented in the javadoc.
        relocate, rename and refactor MathUtils.median(X) to Utils.ithElement(X,X.size()/2)
        In addition, the current median implementation sorts the whole input list witch is O(n log n). However find out the ith element (thus calculate the median) can be done in O(n)
        */
        if (array == null)
            throw new IllegalArgumentException("Array must be non-null");
        final int size = array.size();
        if (size == 0)
            throw new IllegalArgumentException("Array cannot have size 0");
        else if (size == 1)
            return array.get(0);
        else {
            final ArrayList<T> sorted = new ArrayList<>(array);
            Collections.sort(sorted);
            return sorted.get(size / 2);
        }
    }
}

Related

  1. getMedian(List list)
  2. getMedian(List numbers)
  3. getMedian(List results)
  4. getMedianValue(List nums)
  5. median(ArrayList values)
  6. median(List data)
  7. median(List list)
  8. median(List list)
  9. median(List p)