elle
elle

Reputation: 33

How to make the median in java

What am I doing wrong?

The median is always -0.5 result or 0.5, if ((m) + (m+1))/2;

public static double mediana(List<Double> liczby ){
    Collections.sort(liczby);
    int n = liczby.size()/2;
    double m;
    m = get(n);
    if (liczby.size() % 2 == 0){

        return  ((m) + (m-1))/2;

    }
    else {
        return m; 
    }
}

Upvotes: 2

Views: 705

Answers (5)

Fabiano Tarlao
Fabiano Tarlao

Reputation: 3232

I faced the same problem yesterday and I wrote a solution similar to sifho's one. My method---implemented using Java generics---calculates the median value on every collection of Numbers. You can use the method with collections of Doubles, Integers, Floats and returns a double. Please consider that my method creates another collection in order to not alter the original one. I provide also a test, have fun. ;-)

public static <T extends Number & Comparable<T>> double median(Collection<T> numbers){
    if(numbers.isEmpty()){
        throw new IllegalArgumentException("Cannot compute median on empty array of numbers");
    }
    List<T> numbersList = new ArrayList<>(numbers);
    Collections.sort(numbersList);
    int middle = numbersList.size()/2;
    if(numbersList.size() % 2 == 0){
        return 0.5 * (numbersList.get(middle).doubleValue() + numbersList.get(middle-1).doubleValue());
    } else {
        return numbersList.get(middle).doubleValue();
    }

}

JUnit test code snippet:

/**
 * Test of median method, of class Utils.
 */
@Test
public void testMedian() {
    System.out.println("median");
    Double expResult = 3.0;
    Double result = Utils.median(Arrays.asList(3.0,2.0,1.0,9.0,13.0));
    assertEquals(expResult, result);
    expResult = 3.5;
    result = Utils.median(Arrays.asList(3.0,2.0,1.0,9.0,4.0,13.0));
    assertEquals(expResult, result);
}

Usage example (consider the class name is Utils):

List<Integer> intValues = ... //omitted init
Set<Float> floatValues = ... //omitted init
.....
double intListMedian = Utils.median(intValues);
double floatSetMedian = Utils.median(floatValues);

Upvotes: 1

elle
elle

Reputation: 33

I do not know how to look method get(n). It generates automatically. The method can not return to zero.

private static double get(int n) {
    // TODO Auto-generated method stub
    return 0;
}

Upvotes: 0

sifho
sifho

Reputation: 665

In your code, the problem is in this line.

return  ((m) + (m-1))/2;

It should return the average of nth number and (n-1)th number as n = (size of the list)/2. You can try this.

public static double mediana(List<Double> liczby ){
Collections.sort(liczby);
int n = liczby.size()/2;
double m;
if (liczby.size() % 2 == 0)
m = (liczby.get(n) + liczby.get(n-1))/2;
else
m = liczby.get(n);
return m;
}

Upvotes: 3

Andy Turner
Andy Turner

Reputation: 140319

You need to be retrieving the n and n-1-th elements. You are currently subtracting 1 from the n-th value, which is not meaningful:

return (get(n) + get(n-1)) / 2;

Upvotes: 1

TheNumberOne
TheNumberOne

Reputation: 444

I believe the problem with it is the line return ((m) + (m-1))/2; You forget to retrieve the input of the next element in the list. Try:

l = get(n+1);
return (m + l)/2;

instead of:

return  ((m) + (m-1))/2;

Upvotes: 1

Related Questions