Move the highest value of an Array to the end of it. [duplicate]

-1

I need to create a method that takes the highest value of an Array and passes it to the end of that Array so that the array is sorted without losing any value. I've tried it in many ways and still can not. Please correct my code.

My code:

public static void deslocaMaiorFinal(int[] Arranjo){
    int maior = Arranjo[0];
    int aux;
    for(int c = 1; c < Arranjo.length; c++){
        if(Arranjo[c] > maior) maior = Arranjo[c];
    }//end for

    for(int c = (Arranjo.length - 1); c >= 0; c--){
        if(Arranjo[Arranjo.length-1] != maior){
            aux = Arranjo[c];
            Arranjo[c] = maior;
            Arranjo[c-1] = aux;
        }//end if
    }//end for
    return;
}//end method
    
asked by anonymous 18.04.2018 / 19:24

1 answer

0

You do not need the second part:

for(int c = (Arranjo.length - 1); c >= 0; c--){
   if(Arranjo[Arranjo.length-1] != maior){
       aux = Arranjo[c];
       Arranjo[c] = maior;
       Arranjo[c-1] = aux;
   }//end if
}//end for

If you have already identified the highest value (as you did in the first part) just assign it to the last position.

Arranjo[Arranjo.length-1] = maior;
    
18.04.2018 / 19:37