1

Hello I'm trying to make a code that takes three integers from the command line and sorts them into the min, mid, and max values. I can't figure out the mid programming. It won't always sort them properly. Can you help?

public class SortInteger{

public static int max3(int a, int b, int c) {
       int max = a;
       if (b > max) max = b;
       if (c > max) max = c;
       return max;
    }

public static int min3(int a, int b, int c) {
       int min = a;
       if (b < min) min = b;
       if (c < min) min = c;
       return min;}

public static int sort(int a, int b, int c){
    int sort = a;
        if (sort > b && sort < c) sort = a;
        else sort = b;
        if (sort > a && sort < c) sort = b;
        else sort =c;
        if (sort > c && sort < a) sort = c;
        else sort =b;
        if (sort > c && sort < b) sort = c;
        else sort = b;
        if (sort > a && sort < b) sort = c;
        else sort = c;
        return sort;
        }


public static void main(String[] args){
    int a= Integer.parseInt(args [0]);
    int b=Integer.parseInt(args[1]);
    int c=Integer.parseInt(args[2]);
    StdOut.println("Min is " + min3(a, b, c));
    StdOut.println("Mid is " + sort(a, b, c));
    StdOut.println("Max is " +  max3(a, b, c));


}

}

2
  • 1
    Seeing ure use of arg[0], arg[1] ... I take it your familiar with arrays. They will make your life incredibly easy Oct 9, 2015 at 2:21
  • I'm a little rough at arrays. Thanks Guys!
    – spamhair
    Oct 10, 2015 at 23:16

3 Answers 3

4

Try:

public static int mid(int a, int b, int c){
  return a + b + c - max(a,b,c) - min(a,b,c);
}

Also for the min and max just use Math:

public static int min(int a, int b, int c){
  return Math.min(Math.min(a,b),c);//Replace with Math.max for max.
}
1
  • This is the nicest way to do it, although I would not create a method for it. The cast to int is unnecessary, java.lang.Math overloads min and max to take all kind of primitive types : docs.oracle.com/javase/7/docs/api/java/lang/…
    – Dici
    Oct 9, 2015 at 2:28
1

You're stepping all over your own toes inside the sort function. Take, for instance, your first two if statements:

if (sort > b && sort < c) sort = a;
else sort = b;
if (sort > a && sort < c) sort = b;
else sort =c;

If a is between b and c, your first if statement will be true, and sort will be kept as the value of a. But, then consider your next one. The value in a will not be greater than a, so the second if statement will be false, and change sort to c, even though you already found a to be the middle value. Not what you wanted. To fix this, you could change the code you execute when your if statements are true to just return the value of sort. So, like:

if (sort > b && sort < c) return sort;
else sort = b;
if (sort > a && sort < c) return sort;
else sort =c;
// etc.
1
  • Good to have stepped through the code of the OP to show why it's wrong instead of providing a totally different solution (which is also useful). +1
    – Dici
    Oct 9, 2015 at 2:32
0

Try the following:

public class SortInteger
{
    //use general sorting algorithm for arbitrary length, this is for 3 length specifically
    public static int[] sort(int[] inputs)
    {
        int k;
        if(inputs[0] >= inputs[1])
        {
            k = inputs[0];
            inputs[0] = inputs[1];
            inputs[1] = k;
        }

        if(inputs[1] >= inputs[2])
        {
           k = inputs[1];
           inputs[1] = inputs[2];
           inputs[2] = k;
        }

        //incase our last element is less than our first we repeat:

        if(inputs[0] >= inputs[1])
        {
            k = inputs[0];
            inputs[0] = inputs[1];
            inputs[1] = k;
        }

        return inputs
    }

    public static void main(String[] args)
    {
        int[] x = new int[3];
        x[0] = Integer.parseInt(args[0]);
        x[1] = Integer.parseInt(args[1]);
        x[2] = Integer.parseInt(args[2]);
        x = sort(x);

       System.out.println("min is: " + x[0]);
       System.out.println("mid is: " + x[1]);
       System.out.println("max is: " + x[2]);
    }


}
5
  • 1
    Why make it that complicated?
    – user5000849
    Oct 9, 2015 at 2:36
  • Seemd intuitive enough, perhaps i've been brainwashed, but making a sort method over the array and the spitting that out feels naturally. Some of the tricks given above are cool, but not the "simplest way to think" as much as "simplest to code" Oct 9, 2015 at 2:38
  • They are simpler to think about than the above.
    – user5000849
    Oct 9, 2015 at 2:39
  • Thats subjective, i'm sure a lack of upvotes will defend your position, but until then its better to leave it. As it stands this is the only solution that hints at how to do it with arrays. Should the OP get curious about extending to 4,5 etc.. element lists, this will become the only reasonable tactic, so yes its not the cleanest in this case, but it has its place. Oct 9, 2015 at 2:42
  • Agreed, it would not be clean if the above solutions were applied to more than 3 values.
    – user5000849
    Oct 9, 2015 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.