Minimize the program to compute sorting in descending order w/ only using if,switch,operators in java?

import java.util.Scanner;  
  
public class Egone {  
    public static void main(String[] args) {  
        Scanner input = new Scanner(System.in);  
        System.out.println("Enter three integers");  
        int a = input.nextInt();  
        int b = input.nextInt();  
        int c = input.nextInt();  
        int one, two, three;  
        one = two = three = 0;  
        if (a > b && a > c) one = a;  
        else if (b > c && b > a) one = b;  
        else if (c > a && c > b) one = c;  
          
        // for second largest and third largest  
  
        if (one == a) {  
            if (b > c) {  
                two = b;  
            } else {  
                two = c;  
            }  
            if (two == b) {  
                three = c;  
            } else {  
                three = b;  
            }  
        }  
        if (one == b) {  
            if (a > c) {  
                two = a;  
            } else {  
                two = c;  
            }  
            if (two == a) {  
                three = c;  
            } else {  
                three = a;  
            }  
        }  
        if (one == c) {  
            if (a > b) {  
                two = a;  
            } else {  
                two = b;  
            }  
            if (two == a) {  
                three = b;  
            } else {  
                three = a;  
            }  
        }  
  
  
        System.out.println("Sorted order: " + one + " " + two + " " + three);  
  
    }  
  
}

This is my code and I feel most of this code is redundant. I am not sure if it passes all test cases either. Anyone can help me minimize this code?

Hi @Ihattaren,

you could try this as a first idea:

int tmp;
if (a < b) { tmp = a; a = b; b = tmp; } // swap a, b
if (b < c) { tmp = b; b = c; c = tmp; } // swap b, c
// now c is minimal (why?)
if (a < b) { tmp = a; a = b; b = tmp; } // swap a, b again
System.out.println(a + " " + b + " " + c);

Of course you could write a function for swapping two vars.

1 Like

a=a ^ b; b=a ^ b; a=a ^ b
or
a=a+b; b=a-b; a=a-b

1 Like

I minimized this to this:

import java.util.Scanner;

public class Egone {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter three integers");
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		int one, two, three;
		one = two = three = 0;
		if (a > b && a > c) {
			one = a;
			if (b > c) {
				two = b;
				three = c;

			} else {
				two = c;
				three = b;

			}

		}

		else if (b > c && b > a) {
			one = b;
			if (a > c) {
				two = a;
				three = c;

			} else {
				two = c;
				three = a;

			}
		} else if (c > a && c > b) {
			one = c;
			if (a > b) {
				two = a;
				three = b;

			} else {
				two = b;
				three = a;

			}

		}

		System.out.println("Sorted order: " + one + " " + two + " " + three);

	}

}

This optimizes the redundant cases.

shouldn't this be just
} else {
i.e. catching all the other cases including the a==b==c ?

There is no redundancy. It only looks like that because the 3rd check is the same as the 1st one. However, a and b can then have different values than at the 1st check.

Btw, your code doesn't work if at least 2 numbers are equal.

@munkeHoller: I know these swap techniques, but they are not really efficient, cause they need 3 operations resp. operators. The only advantage is that they save a helper var.

i doubt they're inefficient, and same number of ops as having an intermediate variable.
anyways, it was given as an example.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.