In the three variables a, b, and c, three integer pairs of unequal numbers are explicitly written by the programmer. Create a program that rearranges the numbers in variables so that when displaying a sequence of a, b and c was strictly increasing.

  Input example:
 a = 14;
 b = 10;
 c = 23;

 Result:
 a = 10;
 b = 14;
 c = 23;

I did like this

public static void main(String args []){ int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int x; System.out.println("Числа в переменных a, b и c : " + a + " " + b + " " + c); if (a > b) { x = a; a = b; b = x; } if (b > c){ x = b; b = c; c = x; } System.out.println("Возрастающая последовательность: " + a + " " + b + " " + c); } 
  • What have you tried and what have you failed? - Alexey Shimansky
  • Thank you now. - Rubik
  • one
    Add a question code. What difficulties arose in solving the problem? - 0xdb
  • In short, you need to correctly a and b again to compare the first if copied. - pavel
  • But it is not easier to immediately output in this form: Минимальное - мин(а, мин(б, в)). Среднее - (а + б + в - макс(а, макс(б, в)) - мин(а, мин(б, в)). Самое большое - макс(а, макс(б, в)) Минимальное - мин(а, мин(б, в)). Среднее - (а + б + в - макс(а, макс(б, в)) - мин(а, мин(б, в)). Самое большое - макс(а, макс(б, в)) ? - entithat

5 answers 5

Here is a simple solution:

 import java.util.Arrays; public class Sort { public static void main(String[] args) { int[] array = new int[]{21,9,11}; Arrays.sort(array); System.out.println(Arrays.toString(array)); } } 

Conclusion: [9, 11, 21]

If the meaning of the question comes down - how to implement the algorithm yourself, then look, for example, here and reinvent the wheel.

  • for a fixed number of elements, you can use specialized sorting algorithms (if there are specialized requirements, for example, as in the question body or for speed ) - jfs
  • @jfs Yes, I know. And although I suspect that this is a control task or something like that and a couple of written cycles are expected, but in the question I did not see any special requirements. Let the author himself try to solve the problem. - 0xdb
  • under the "specialized requirements" in the question body, I meant that the input and output are given in three separate variables, and not in one array, if you follow the letter. - jfs

It has been proven ( for sorting networks ) that three comparisons are the optimal number, but your code in question uses only two comparisons (error). Add a third comparison, for example:

 public class Sort3 { public static void main(String[] args) { int a = 14; int b = 10; int c = 23; assert a != b && a != c && b != c; System.out.println("Числа в переменных a, b и c : " + a + " " + b + " " + c); if (b > c) { int x = b; b = c; c = x; // swap(b, c); } assert b < c; if (a > c) { int x = a; a = c; c = x; // swap(a, c); } assert a < c; if (a > b) { int x = a; a = b; b = x; // swap(a, b); } assert a < b && b < c; System.out.println("Возрастающая последовательность: " + a + " " + b + " " + c); } } 

Example:

 $ javac -g Sort3.java $ java -ea Sort3 Числа в переменных a, b и c : 14 10 23 Возрастающая последовательность: 10 14 23 

In most cases, Arrays.sort(array) should Arrays.sort(array) standard function , as shown in the answer @ 0xdb , since the version created by the hands may contain errors (as your example showed), it may be less clear (there is no obvious intention compared to with Arrays.sort(array) ) and may even be slower ( always measure if performance is important ).

    The algorithm is as follows: at the beginning you need to create a variable (min) and write one of your numbers there. Then in the cycle compare it with each subsequent one. If some turned out to be less, then you change the value of min to the one that was less when compared. And so at the end of the cycle in min you will have that variable below which you did not find. Then repeat this for the maximum number. The third number will be average.

    Here is a sketch of the code:

     int[] array = {a, b, c}; var min = array[0]; for (var i; i < 3; i++) { if (min > array[i]) min = array[i] } 
    • I do not understand people who are minus. This is the basis of algorithmization - Nikolay
    • one
      Apparently for the work of the author, because you move a person to just come for copy-paste without your own efforts in solving. - Alexey Shimansky
    • There was a question, I answered, I do not understand the problem. There is no ready code. It obviously needs to be adjusted, here only finding the minimum and that's it. And if there was a bare code without an explanation, then there could be another reason for a minus, but there is an explanation. Complete and with an example. I think someone just did not have time to answer and decided to instruct the cons: \ - Nikolay
    • The number of answers is not limited here, actually)) Moreover, even one participant can write many answers) - Alexey Shimansky
    • I meant the first gave the answer. Then my logic is broken - Nikolay

    If there are only 3 variables, then:

     public static void main(String args []){ int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int min = Math.min(Math.min(a,b),c) int max = Math.max(Math.max(a,b),c) b = (a + b + c) - min - max; a = min; c = max; System.out.println("Возрастающая последовательность: " + a + " " + b + " " + c); } 

    Ps. if there are more variables, then, of course, it is necessary through arrays ...

       public static void main(String[] args) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int x; if (a > b) { x = a; a = b; b = x;} else a = a; if (b > c){ x = b; b = c; c = x;} else b=b; if (a > b) { x = a; a = b; b = x;} else c= c; System.out.println("Возрастающая последовательность:" + a + " " + b + " " + c); } } 
      • And what code do you write? What is it wavy. - 0xdb
      • one
        if you add a couple of variables, then such code will be a bit cumbersome and Hindu. Not the best practice - Nikolay
      • I write on eclipse - Rubik
      • In eklipse autoindent is, why not use. And you added the code to the question, t.ch. You can delete this answer. - 0xdb