How to calculate the median of an array?

后端 未结 14 1675
北海茫月
北海茫月 2020-12-05 06:10

I\'m trying to calculate the total, mean and median of an array thats populated by input received by a textfield. I\'ve managed to work out the total and the mean, I just ca

相关标签:
14条回答
  • 2020-12-05 06:50

    I was looking at the same statistics problems. The approach you are thinking it is good and it will work. (Answer to the sorting has been given)

    But in case you are interested in algorithm performance, I think there are a couple of algorithms that have better performance than just sorting the array, one (QuickSelect) is indicated by @bruce-feist's answer and is very well explained.

    [Java implementation: https://discuss.leetcode.com/topic/14611/java-quick-select ]

    But there is a variation of this algorithm named median of medians, you can find a good explanation on this link: http://austinrochford.com/posts/2013-10-28-median-of-medians.html

    Java implementation of this: - https://stackoverflow.com/a/27719796/957979

    0 讨论(0)
  • 2020-12-05 06:50

    And nobody paying attention when list contains only one element (list.size == 1). All your answers will crash with index out of bound exception, because integer division returns zero (1 / 2 = 0). Correct answer (in Kotlin):

    MEDIAN("MEDIAN") {
    
            override fun calculate(values: List<BigDecimal>): BigDecimal? {
                if (values.size == 1) {
                    return values.first()
                }
                if (values.size > 1) {
                    val valuesSorted = values.sorted()
                    val mid = valuesSorted.size / 2
                    return if (valuesSorted.size % 2 != 0) {
                        valuesSorted[mid]
                    } else {
                        AVERAGE.calculate(listOf(valuesSorted[mid - 1], valuesSorted[mid]))
                    }
                }
                return null
            }
        },
    
    0 讨论(0)
  • 2020-12-05 06:52

    If you want to use any external library here is Apache commons math library using you can calculate the Median.
    For more methods and use take look at the API documentation

    import org.apache.commons.math3.*;
    .....
    ......
    ........
    //calculate median
    public double getMedian(double[] values){
     Median median = new Median();
     double medianValue = median.evaluate(values);
     return medianValue;
    }
    .......
    
    • For more on evaluate method AbstractUnivariateStatistic#evaluate

    Update

    Calculate in program

    Generally, median is calculated using the following two formulas given here

    If n is odd then Median (M) = value of ((n + 1)/2)th item term.
    If n is even then Median (M) = value of [((n)/2)th item term + ((n)/2 + 1)th item term ]/2

    In your program you have numArray, first you need to sort array using Arrays#sort

    Arrays.sort(numArray);
    int middle = numArray.length/2;
    int medianValue = 0; //declare variable 
    if (numArray.length%2 == 1) 
        medianValue = numArray[middle];
    else
       medianValue = (numArray[middle-1] + numArray[middle]) / 2;
    
    0 讨论(0)
  • 2020-12-05 06:55
    Arrays.sort(numArray);
    int middle = ((numArray.length) / 2);
    if(numArray.length % 2 == 0){
     int medianA = numArray[middle];
     int medianB = numArray[middle-1];
     median = (medianA + medianB) / 2;
    } else{
     median = numArray[middle + 1];
    }
    

    EDIT: I initially had medianB setting to middle+1 in the even length arrays, this was wrong due to arrays starting count at 0. I have updated it to use middle-1 which is correct and should work properly for an array with an even length.

    0 讨论(0)
  • 2020-12-05 06:56

    Try sorting the array first. Then after it's sorted, if the array has an even amount of elements the mean of the middle two is the median, if it has a odd number, the middle element is the median.

    0 讨论(0)
  • 2020-12-05 06:59

    The Arrays class in Java has a static sort function, which you can invoke with Arrays.sort(numArray).

    Arrays.sort(numArray);
    double median;
    if (numArray.length % 2 == 0)
        median = ((double)numArray[numArray.length/2] + (double)numArray[numArray.length/2 - 1])/2;
    else
        median = (double) numArray[numArray.length/2];
    
    0 讨论(0)
提交回复
热议问题