Given three integers, print them in sorted order without using if condition.
Examples : 
 
Input : a = 3, b = 2, c = 9 Output : 2 3 9 Input : a = 4, b = 1, c = 9 Output : 1 4 9
Approach : 
1. Find the maximum of a, b, c using max() function. 
3. Multiply all integers by –1. Again find Minimum of –a, –b, –c using max() function. 
4. Add the Max and Min from above steps and subtract the sum from (a+b+c). It gives us middle element.
It works for negative numbers also.
 
C++
| // C++ program to print three numbers  // in sorted order using max function #include<bits/stdc++.h> usingnamespacestd;  voidprintSorted(inta, intb, intc) {     // Find maximum element     intget_max = max(a, max(b, c));      // Find minimum element     intget_min = -max(-a, max(-b, -c));      intget_mid = (a + b + c)                  - (get_max + get_min);      cout << get_min << " "<< get_mid         << " "<< get_max; }  // Driver code intmain() {     inta = 4, b = 1, c = 9;     printSorted(a, b, c);     return0; }  | 
Java
| // Java program to print three numbers  // in sorted order using max function classGFG  {          staticvoidprintSorted(inta, intb, intc)     {         // Find maximum element         intget_max = Math.max(a, Math.max(b, c));              // Find minimum element         intget_min = -Math.max(-a, Math.max(-b, -c));              intget_mid = (a + b + c)                        - (get_max + get_min);              System.out.print(get_min + " "+ get_mid                                 + " "+ get_max);     }          // Driver code     publicstaticvoidmain(String[] args)     {                  inta = 4, b = 1, c = 9;                  printSorted(a, b, c);     } }  // This code is contributed by Anant Agarwal.  | 
Python3
| # Python3 program to print three numbers  # in sorted order using max function  defprintSorted(a, b, c):          # Find maximum element     get_max =max(a, max(b, c))      # Find minimum element     get_min =-max(-a, max(-b, -c))      get_mid =(a +b +c) -(get_max +get_min)      print(get_min, " ", get_mid, " ", get_max)  # Driver Code a, b, c =4, 1, 9printSorted(a, b, c)  # This code is contributed by Anant Agarwal.  | 
C#
| // C# program to print three numbers  // in sorted order using max function usingSystem;  classGFG {          staticvoidprintSorted(inta, intb, intc)     {         // Find maximum element         intget_max = Math.Max(a, Math.Max(b, c));              // Find minimum element         intget_min = -Math.Max(-a, Math.Max(-b, -c));              intget_mid = (a + b + c) -                       (get_max + get_min);          Console.Write(get_min + " "+ get_mid                           + " "+ get_max);     }          // Driver code     publicstaticvoidMain()     {         inta = 4, b = 1, c = 9;                  printSorted(a, b, c);     } }  // This code is contributed by nitin mittal.  | 
PHP
| <?php // PHP program to print three numbers  // in sorted order using max function  functionprintSorted($a, $b, $c) {          // Find maximum element     $get_max= max($a, max($b, $c));      // Find minimum element     $get_min= -max(-$a, max(-$b, -$c));      $get_mid= ($a+ $b+ $c) -                 ($get_max+ $get_min);      echo$get_min, " ", $get_mid, " ", $get_max; }      // Driver Code     $a= 4;     $b= 1;      $c= 9;     printSorted($a, $b,$c);  // This code is contributed by nitin mittal. ?>  | 
Javascript
| <script>  // JavaScript program for the above approach functionprintSorted(a, b, c)      {               // Find maximum element          let get_max = Math.max(a, Math.max(b, c));                 // Find minimum element          let get_min = -Math.max(-a, Math.max(-b, -c));                 let get_mid = (a + b + c)                         - (get_max + get_min);                 document.write(get_min + " "+ get_mid                                  + " "+ get_max);      }   // Driver Code           let a = 4, b = 1, c = 9;               printSorted(a, b, c);   // This code is contributed by splevel62. </script>  | 
Output: 
 
1 4 9
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Rakesh Kumar. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







