1

The question is to Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables num1, num2, and num3, respectively. The program sorts the numbers so that num1 <= num2 <= num3.

actually I do that but the result is available only to 1 ,2 and 3 numbers !

When I enter any different number it doesn't show me the result I want it !

here is my code..

import javax.swing.JOptionPane;

public class number order {

 public static void main(String[] args) {

 int num1;
 int num2;
 int num3;

    String n = JOptionPane.showInputDialog(null, "input NUM 1 " );
           num1 = Integer.parseInt(n);
    String u = JOptionPane.showInputDialog(null, "input NUM 2 " );
         num2 = Integer.parseInt(u);
    String m = JOptionPane.showInputDialog(null, "input NUM 3 " );
        num3 = Integer.parseInt(m);

    if (num1<=num2&& num2<=num3)
        System.out.println( num1+"<="+ num2+"<="+num3 );
    if(num2<=num1&&num1<=num3)
        System.out.println(num2+"<="+num1+"<="+num3);
    if (num3<=num1&&num1<=num2)
        System.out.println(num3+"<="+num1+"<="+num2);


    // TODO code application logic here
 }
}
11
  • Could you rephrase your question? Do you mean that the program can only accept 3 numbers as input (and not, e.g., four or five)? Or do you mean that the "sorting" works only for certain inputs but not for others? Commented Oct 18, 2013 at 8:06
  • I mean the sorting works only for certain inputs which are ( 1,2 and 3 ) .. Commented Oct 18, 2013 at 8:13
  • Just to make this clear, if you input, say, 6, 2, and 9, the sorting does not work? Commented Oct 18, 2013 at 8:14
  • yes exactly and when I input 1,2and 3 it works .. 1<=2<=3 .. but other numbers are not Commented Oct 18, 2013 at 8:17
  • 1
    Could you check this again? There's nothing in your code which would restrict input to integers 1,2,3. In fact, 6,2,9 (in this order) should be sorted just fine. Commented Oct 18, 2013 at 8:47

5 Answers 5

1

The problem is that you check only three out of six possible arrangements of those three numbers. Also note that, even for those three, you are not actually sorting the numbers, but only printing them in sorted order, i.e., you are never reassigning the variables num1, num2, and num3.

Just as an alternative to checking all the possible arrangements of the three numbers or implementing a full sorting algorithm, you can also compare and swap pairs of numbers. This way, you get away with far fewer comparisons while still being able to sort all permutations of three numbers.

  • if num1 > num2, swap num1 and num2
  • if num2 > num3, swap num2 and num3
  • if num1 > num2, swap num1 and num2 again

After those three swaps, the numbers are in sorted order.

Of course, if you have more than three numbers this gets impractical, and you should rather implement a full sorting algorithm (for exercise) or go with one of the builtins, like Arrays.sort (for real life).

Sign up to request clarification or add additional context in comments.

Comments

0

You haven't checked all cases:

1 < 2 < 3
1 < 3 < 2
2 < 1 < 3
2 < 3 < 1
3 < 1 < 2
3 < 2 < 1

However to check all case like that is not to useful. an sorted array would be more easy:

int arr[3]={num1,num2,num3}
java.utils.Arrays.sort(arr);
println(arr[0] + "<=" + arr[1] + "<=" + arr[2]);

Comments

0
int[] all = new int[]();
num1 = Integer.parseInt(n);
all[0] = num1;
num2 = Integer.parseInt(u);
all[1] = num1;
num3 = Integer.parseInt(m);
all[2] = num1;
for(int i=0; i <all.length ;i++){
  if(i!=0 && all[i]< all[i-1]){
     temp = all[i-1];
     all[i-1] = all[i];
     all[i] = temp; 
  }
}

all will have sorted array
or more simply

2 steps
1)add all to a array.
2) call Arrays.sort(array)

1 Comment

Is my code write ? I just need to work in JOptionPane method ! and thanks so much ..
0

how about

int min = min(min(num1,num2),num3);
int max = max(max(num1,num2),num3);
int mid = num1 + num2 + num3 - min - max;
System.out.println(min+"<="+mid+"<="+max);

Comments

0
if(num1 < num2){ if(num3 < num1) System.out.println("num2 > num1 > num3");}
else{ if(num2 < num3) System.out.println("num3 > num2 > num1"); else System.out.println("num1 > num2 > num3");}

Sort in descending order.

Hope it helps.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.