Java BigDecimal

  • + 0 comments

    Used Bubble Sort

    import java.math.BigDecimal; import java.util.*; class Solution{ public static void main(String []args){ //Input Scanner sc= new Scanner(System.in); int n=sc.nextInt(); String []s=new String[n+2]; for(int i=0;i

        //Write your code here
        // Bubble Sort in descending order
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                BigDecimal bg1 = new BigDecimal(s[j]);
                BigDecimal bg2 = new BigDecimal(s[j+1]);
                if (bg2.compareTo(bg1) > 0) {
                    // Swap if the left is smaller than the right
                    String temp = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = temp;
                }
            }
        }
        //Output
        for(int i=0;i<n;i++)
        {
            System.out.println(s[i]);
        }
    }
    

    }