I do not understand a sorting resolution

3

The teacher asked for the following exercise:

"Write a program that gets three scores (integers), sorts those scores using if statements and writes the three in ascending order on the screen. "

I tried to do it my way alone but I ended up getting wrapped up for almost 30 minutes lost in the middle of so many if's and else's and went to see the resolution that the teacher made available, but I can not understand it. The resolution is as follows: / p>

System . out . println("Indique a 1ª pontuação:");
    int pontuacao1=scanner.nextInt();
    System . out . println("Indique a 2ª pontuação:");
    int pontuacao2 = scanner.nextInt();
    System . out . println("Indique a 3ª pontuação:");
    int pontuacao3 = scanner.nextInt();

     if (pontuacao2 < pontuacao1) 
    { 
        int valor = pontuacao1; 
        pontuacao1 = pontuacao2; 
        pontuacao2 = valor; 
    } 
    if (pontuacao3 < pontuacao2) 
    { 
        int valor = pontuacao2; 
        pontuacao2 = pontuacao3; 
        pontuacao3 = valor; 
        if (pontuacao2 < pontuacao1) 
        { 
            int valor2 = pontuacao1; 
            pontuacao1 = pontuacao2; 
            pontuacao2 = valor; 
        } 
    } 

    System.out.print(pontuacao1 + ", " + pontuacao2 + ", " + pontuacao3);

The first part of input values I realize but from the if not. Can anyone explain?

    
asked by anonymous 28.10.2017 / 23:29

1 answer

3

Temporary variable

The variable valor serves to temporarily store a value so as not to lose it.

Come on,

We get three values, for example suppose: 11, 5, 3 .

Now we need to sort them, so let's make the comparisons with the conditional structures if :

if (pontuacao2 > pontacao1) : Let's compare of the second score is greater than the first. In our case not right? So let's ask the next if ,

if (pontuacao3 < pontuacao2) : Is the third score smaller than the second? YEA. Then we went into the structure,

Since the last position is smaller than the second position they should be changed right? Yes. But if we just do it:

pontuacao2 = pontuacao3;
pontacao3 = pontuacao2;

What will happen? Let's think:

// pontuacao2 will get the value of pontuacao3 , then pontuacao2 will become 3 ,

// Now pontuacao 3 gets the value of pontuacao2 , but notice, pontuacao2 is now 3 , so the two variables will end up having the 3 value.

Where did our 5 go? In that case we lost him. So from a temporary variable, so we can temporarily hold the 5 to give it to the other pontuacao . Let's see:

int valor = pontuacao2; 
pontuacao2 = pontuacao3; 
pontuacao3 = valor; 

// Now temporarily store the value of pontuacao2 in valor , in this case, 5

// Then we pass the value from pontuacao3 to pontuacao2 , it gets 3  // Finally, we still have our 5 saved, in valor , now we just move it to pontuacao3 . Ready.

Just repeat this for the next condition: pontuacao2 < pontuacao1 , which in this case is also true.

Now let's think about the structure of if

We first have a question, if pont2 < pont1. We must ask this question, in the case of a true condition, we enter the if and invert the positions.

Now we have another question, if pont3 < pont2, why in this case we use another if instead of else if ? We use another if because we want to perform this check regardless of the result of the first one.

Now, why is the third if within the second? Note, this condition will ONLY be tested if the second is valid. If pont2 is not less than pont1 and pont3 is not less than pont2 it means that the numbers are already sorted, so performing a third check would be unnecessary, so we ended up optimizing our code.

    
28.10.2017 / 23:53