0

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);
3
  • 3
    putting everything into an array and loop over it is no option? I mean, nobody would do it in that way you showed. Commented Nov 8, 2018 at 7:32
  • Are additional variables allowed? For example, could I have a flag that tells me whether the max is in a or e? (I will say no more, as that would risk you committing plagiarism.) Commented Nov 8, 2018 at 7:43
  • to get 6 comparaisonto find max and min you mustn't sort the array but just searching if its a max it isn't a min and use else to manage your case Commented Nov 8, 2018 at 7:51

4 Answers 4

3

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.

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

Comments

0

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);

Comments

0

// 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 ; }

Comments

-1

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)); 

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.