Java Median median(ArrayList v)

Here you can find the source of median(ArrayList v)

Description

Return the median

License

Open Source License

Declaration

static public double median(ArrayList<Double> v) 

Method Source Code


//package com.java2s;
/*/*from  w ww  . ja va2s  .co  m*/
Copyright (c) 2008-2010 Daniel Marbach & Thomas Schaffter
    
We release this software open source under an MIT license (see below). If this
software was useful for your scientific work, please cite our paper(s) listed
on http://gnw.sourceforge.net.
    
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.ArrayList;
import java.util.Collections;

public class Main {
    /** Return the median */
    static public double median(ArrayList<Double> v) {

        Collections.sort(v);

        if (v.size() % 2 == 1)
            return v.get((v.size() + 1) / 2 - 1);
        else {
            double lower = v.get(v.size() / 2 - 1);
            double upper = v.get(v.size() / 2);

            return (lower + upper) / 2.0;
        }
    }
}

Related

  1. getMedian(double array[])
  2. getMedian(double[] a)
  3. getmedian(double[] vals, int nvalindex)
  4. getMedian(int[] array)
  5. getMedianValue(ArrayList values)
  6. median(ArrayList values)
  7. Median(ArrayList values)
  8. median(double... a)
  9. median(double[] a)