Sort 3 Integers without using if condition or using only max() function

Given three inte­gers, print them in sorted order with­out 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 max­i­mum of a, b, c using max() function.
3. Mul­ti­ply all inte­gers by –1. Again find Min­i­mum of –a, –b, –c using max() function.
4. Add the Max and Min from above steps and sub­tract the sum from (a+b+c). It gives us mid­dle element.

It works for negative numbers also.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print three numbers 
// in sorted order using max function
#include<bits/stdc++.h>
using namespace std;
  
void printSorted(int a, int b, int c)
{
    // Find maximum element
    int get_max = max(a, max(b, c));
  
    // Find minimum element
    int get_min = -max(-a, max(-b, -c));
  
    int get_mid = (a + b + c)
                 - (get_max + get_min);
  
    cout << get_min << " " << get_mid
        << " " << get_max;
}
  
// Driver code
int main()
{
    int a = 4, b = 1, c = 9;
    printSorted(a, b, c);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print three numbers 
// in sorted order using max function
class GFG 
{
      
    static void printSorted(int a, int b, int c)
    {
        // Find maximum element
        int get_max = Math.max(a, Math.max(b, c));
      
        // Find minimum element
        int get_min = -Math.max(-a, Math.max(-b, -c));
      
        int get_mid = (a + b + c) 
                      - (get_max + get_min);
      
        System.out.print(get_min + " " + get_mid
                                + " " + get_max);
    }
      
    // Driver code
    public static void main(String[] args)
    {
          
        int a = 4, b = 1, c = 9;
          
        printSorted(a, b, c);
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print three numbers 
# in sorted order using max function
  
def printSorted(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, 9
printSorted(a, b, c)
  
# This code is contributed by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print three numbers 
// in sorted order using max function
using System;
  
class GFG {
      
    static void printSorted(int a, int b, int c)
    {
        // Find maximum element
        int get_max = Math.Max(a, Math.Max(b, c));
      
        // Find minimum element
        int get_min = -Math.Max(-a, Math.Max(-b, -c));
      
        int get_mid = (a + b + c) -
                      (get_max + get_min);
      
    Console.Write(get_min + " " + get_mid
                          + " " + get_max);
    }
      
    // Driver code
    public static void Main()
    {
        int a = 4, b = 1, c = 9;
          
        printSorted(a, b, c);
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print three numbers 
// in sorted order using max function
  
function printSorted($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.
?>

chevron_right



Output:

 1 4 9

This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.



My Personal Notes arrow_drop_up

Improved By : nitin mittal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.