How to order vector in descending order?

Asked

Viewed 20,854 times

4

How do I place a vector in descending order?

    public static void vetor() {
      int[] vet = {
          7,
          4,
          10,
          8,
          2,
          5
      };
      Arrays.sort(vet);
      //Comando para colocar em ordem decrescente.
      for (int i = 0; i < vet.length; i++) {
          System.out.println(vet[i] + "");
      }

  }

4 answers

6


To sort an array in descending order, you can use the sort algorithm Bubble Sort:

for (int i = 1; i < vet.length; i++) {
    for (int j = 0; j < i; j++) {
        if (vet[i] > vet[j]) {
            int temp = vet[i];
            vet[i] = vet[j];
            vet[j] = temp;
        }
    }
}

They are two loops that compare the values of two in two of the array, when a value in the later position is greater than in the previous position(if (vet[i] > vet[j]), it changes both position. And so it continues to compare up to the last two elements.

See working on IDEONE


Another interesting solution I found in Soen is this:

for(int i=0;i<vet.length;i++) {

    vet[i]=-vet[i];
}

Arrays.sort(vet);

for(int i=0;i<vet.length;i++) {

    vet[i]=-vet[i]; 
}

The code above makes:

  • multiplies all indices of the vector by -1;
  • sort the vector upwards;
  • again multiplies all indices by -1

The result is the same, as can be seen in ideone.

  • 1

    I prefer the second solution: it is made on a sorting algorithm n log(n)

  • I saw that new functionality from Java 8 @diegofm, what do you think?

  • @Marconi to be honest, I never used that lib there. Apache Commons also has a feature to sort array of primitives, but since I have never used it myself, I only put forms using native methods. If you went to an Integer array, you could use it and it would be easier to do this.

  • @diegofm I was curious too much in this lib, as I’m rusty in Java and I don’t have the IDE just gave a read even. I think it would be interesting in your answer!

  • @Marconi I will try to test later, in java8 has some features that I still kind of outdated kkk

  • 1

    @diegofm quiet, already left my +1!

  • We did research in college, using Bubble Sorte, Quick Sort, and others, on Windows 10 and Linux operating systems, with identical and different processors, as they were several academics, the Bubble Sort was the slowest, and put slow in it.

Show 2 more comments

0

How about doing it this way?

Arrays.sort(vet);
int[] vetAux;

int contador = 0;
for(int i=vet.length - 1;i >= 0;i--) {
    vetAux[contador]=vet[i]; 
    contador++;
}

-1

//Printing Array in descending order

public class Main {

public static void main(String[] args) {

    int valor[] = new int[1000];
    for(int i =0;i<valor.length;i++){
        valor[i] = valor.length - i;
        System.out.println(valor[i]);

    }


}

}

-3

//Ordem Decrescente

import java.util.Scanner;
public class OrdemDecrescente {

    public static void main(String[] args) {

     int cont = 1;
     int[] vet = new int[5];

     Scanner sc = new Scanner(System.in);
    for (int i = 0; i < vet.length; i++){  
        System.out.print("Digite "+cont+"° número: \t");
        vet[i] = sc.nextInt();
        cont++;
        }

    for (int i = 1; i < vet.length; i++) {
        for (int j = 0; j < i; j++) {
            if (vet[i] > vet[j]) {
                int aux = vet[i];
                vet[i] = vet[j];
                vet[j] = aux;
            }
        }
    }
    System.out.println("Array em ordem decrescente:");
    for (int n : vet) {
        System.out.print(n + " ");
        }
    }
}
  • What does your answer have different from the answers you’ve posted? I’ve seen nothing different

  • An example of merging pieces of code... nothing more

Browser other questions tagged

You are not signed in. Login or sign up in order to post.