Skip to content

Instantly share code, notes, and snippets.

@chinying
Last active August 29, 2015 14:20
Show Gist options
  • Save chinying/e1b99a6a033455a8457a to your computer and use it in GitHub Desktop.
Save chinying/e1b99a6a033455a8457a to your computer and use it in GitHub Desktop.
so i had this question for my finals today : take 3 integers from the command line and print them in descending order. i did it without sorting :)
class f{
public static int max(int x, int y){
return (x > y) ? x : y;
}
public static int min(int x, int y){
return (x < y) ? x : y;
}
public static void main(String [] args){
int i, hi, mid, lo, x1 = 0, x2 = 0;
if (args.length != 3) return;
int arr[] = new int[3];
for (i=0; i<3; i++) arr[i] = Integer.parseInt(args[i]);
hi = max(max(arr[0], arr[1]), arr[2]);
lo = min(min(arr[0], arr[1]), arr[2]);
for (i=0; i<3; i++) x1^=arr[i];
x2^=hi;
x2^=lo;
mid = x1^x2;
System.out.printf("%d %d %d\n", hi, mid, lo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment