Need help in java algorithm for finding max and min

I need to write a java algorithm that receive 5 numbers from type int and print the max and min by using only 6 times with the Conditional Exchange:

If (x>y) {
    Int tmp = x;
    x=y;
    y=x;
} 

The problem is that I only manage to do this algorithm with 7 conditional exchange and not 6. Can someone help me to understand what I'm missing?

    Scanner myScanner = new Scanner(System.in);
    int a = myScanner.nextInt();
    int b = myScanner.nextInt();
    int c = myScanner.nextInt();
    int d = myScanner.nextInt();
    int e = myScanner.nextInt();

    if(a>b)
    {
        int tmp = b;
        b = a;
        a = tmp;
    }

    if(a>c)
    {
        int tmp = c;
        c = a;
        a = tmp;
    }

    if(a>d)
    {
        int tmp = d;
        d = a;
        a = tmp;
    }

    if(a>e)
    {
        int tmp = e;
        e = a;
        a = tmp;
    }

    if(b>e)
    {
        int tmp = e;
        e = b;
        b = tmp;
    }

    if(c>e)
    {
        int tmp = e;
        e = c;
        c = tmp;
    }

    if(d>e)
    {
        int tmp = e;
        e = d;
        d = tmp;
    }

    System.out.println(a);
    System.out.println(e);

5 answers

  • answered 2018-11-08 07:40 Khalid Shah

    There is a easy way of doing this. There is no need of swapping. If you want to find min and max by if/else then see my example otherwise you should use a array for this.

        Scanner myScanner = new Scanner(System.in);
        int a = myScanner.nextInt();
        int b = myScanner.nextInt();
        int c = myScanner.nextInt();
        int d = myScanner.nextInt();
        int e = myScanner.nextInt();
        int min = a,max=a;
        if(b<min){
             min=b;
        }
        if(c<min){
             min=c;
        }
        if(d<min){
             min=d;
        }
        if(e<min){
             min=e;
        }
    
        if(b>max){
             max=b;
        }
        if(c>max){
             max=c;
        }
        if(d>max){
             max=d;
        }
        if(e>max){
             max=e;
        }
    
        System.out.println(min);
        System.out.println(max);
    

  • answered 2018-11-08 07:47 Aravind Bhat K

    Put Every single element in an Array (arr1), And then import collections and Arrays

    import java.util.Arrays;
    import java.util.Collections; 
    
    int min = Collections.min(Arrays.asList(arr1));  // arr1 will be your array
    int max = Collections.max(Arrays.asList(arr1)); 
    

  • answered 2018-11-08 08:08 Andy Turner

    Think about it like a merge sort: break the problem down to finding the min and max for pairs of numbers; then for pairs of pairs etc.

    if (a >= b) swap(a, b); // logically swap them; you can't actually write a method to do this.
    

    Now a = min(a, b) and b = max(a, b).

    if (c >= d) swap(c, d);
    

    Now c = min(c, d) and d = max(c, d).

    if (a >= c) swap(a, c);
    if (b >= d) swap(b, d);
    

    Now a = min(a, b, c, d) and d = max(a, b, c, d).

    Then it's just a matter of handling e:

    if (a >= e) swap(a, e);
    if (d >= e) swap(d, e);
    

    The min is in a; the max is in e.

  • answered 2018-11-08 08:14 raven1981

    Comparing two pairs first, then you can save a comparison knowing than the first pair smaller must be compared with the second pair smaller and the first pair bigger must be compared with the second pair bigger. We have a number left that must be compared with the current smaller and bigger in case it fits there.

    Implementation:

    Scanner myScanner = new Scanner(System.in);
    int a = myScanner.nextInt();
    int b = myScanner.nextInt();
    int c = myScanner.nextInt();
    int d = myScanner.nextInt();
    int e = myScanner.nextInt();
    
    myScanner.close();
    
    // First pair
    if(a>e)
    {
        int tmp = e;
        e = a;
        a = tmp;
    }
    
    // Second pair
    if(c>d)
    {
        int tmp = d;
        d = c;
        c = tmp;
    }
    
    // Smaller
    if(a>c) {
        int tmp = a;
        a = c;
        c = tmp;
    }
    
    // Bigger
    if(d>e) {
        int tmp = d;
        d = e;
        e = tmp;
    }
    
    // Remaining value bigger
    if(b>e) {
        int tmp = b;
        b = e;
        e = tmp;
    }
    
    // Remaining value smaller     
    if(a>b) {
        int tmp = b;
        b = a;
        a = tmp;
    }
    
    
    
    System.out.println(a);
    System.out.println(e);
    

  • answered 2018-11-08 08:36 MrCode

    // Compare your numbers in pairs, I've paired up a-b and c-d and e will be compared separately

    Scanner myScanner = new Scanner(System.in);
    
    int a = myScanner.nextInt();
    int b = myScanner.nextInt();
    int c = myScanner.nextInt();
    int d = myScanner.nextInt();
    int e = myScanner.nextInt(); 
    
    int min ; int max ;
    int min1 = a, max1 = b;
    int min2 = c, max2 = d ; 
    
    if(a > b ){
          min1 = b ;
          max1 = a ;
          // Here I set original min and max to min1 and max2 to save extra conditionals
          min = min1 ; 
          max = max1 ;  
    }
    
    if(c > d ){
          min2 = b ;
          max2 = a ; 
    }
    
    if(min1 > min2)
          min = min2 ; 
    if(max2 > max1)
          max = max2 ;
    
    // One last comparison for e and we've found what were looking for :)
    if( e > max){ 
          max = e ; }
    if( e < min){
          min = e ; }