Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding First Quartile and Third Quartile in integer array using java

Tags:

java

arrays

public class tryA {

  public static void main(String[] args) {
  int[] intArray= new int[41];
  System.out.println(intArray[intArray.length/2]);

}

How to find the Lower Quartile (Q1) and Third Quartile (Q3) for my integer Array intArray? Providing that the size of array might be a variables.

P.S: It is used to find the outlier of the array.


1 Answers

My solution is plug and play! You just give an array (val) of double numbers and it gives you back an array with 3 doubles Q1 in position 0, Q2 in position 1 and Q3 in position 2.

public double[] Quartiles(double[] val) {
    double ans[] = new double[3];

    for (int quartileType = 1; quartileType < 4; quartileType++) {
        float length = val.length + 1;
        double quartile;
        float newArraySize = (length * ((float) (quartileType) * 25 / 100)) - 1;
        Arrays.sort(val);
        if (newArraySize % 1 == 0) {
            quartile = val[(int) (newArraySize)];
            } else {
            int newArraySize1 = (int) (newArraySize);
            quartile = (val[newArraySize1] + val[newArraySize1 + 1]) / 2;
             }
        ans[quartileType - 1] =  quartile;
    }
    return ans;
}
like image 83
vasilis vittis Avatar answered Dec 15 '25 14:12

vasilis vittis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!