Java BigDecimal

  • + 0 comments

    Check your output: your result puts .12 before 0.12, but it's wrong since it's required that:

    If two numbers represent numerically equivalent values (e.g., ), then they must be listed in the same order as they were received as input).

    So, the sorting algorithm you used is not stable. I tried to fix your code, using the Bubble Sort:

    String tempS;
    for (int i=0; i<n-1; i++) {
        for (int j=0; j<n-i-1; j++) {
            BigDecimal bd1 = new BigDecimal(s[j]);
            BigDecimal bd2 = new BigDecimal(s[j+1]);
            if (bd1.compareTo(bd2)<0) {
                tempS = s[j];
                s[j] = s[j+1];
                s[j+1] = tempS;
            }
        }
    }