Sort ascending array of integers in Java

How to put in ascending order an array of integers in Java? I have a class and Pilha.java Ordenacao.Java, but the program does not work.

Pilha.java

package lista03Pilhas; public class Pilha { // indica qual o tamanho maximo da pilha (tamanho maximo do vetor) private int tamanhoMaximo; // indica o topo da pilha (quantidade de itens do vetor) private int topo; // cria o vetor que ira implementar a pilha private Object[] vetorPilha; // construtor que recebe como parametro o tamanho da pilha (tamanho do vetor) public Pilha(int length) { // indica o tamanho da pilha (vetor) tamanhoMaximo = length; // instancia o vetor com o tamanho informado vetorPilha = new Object[length]; // faz com que o apontador do topo indique que não há elementos na pilha topo = -1; } // funcao que insere um objeto no topo da pilha public void push(Object obj) { // incrementa o topo (desloca para cima) topo++; // coloca o novo objeto na pilha vetorPilha[topo] = obj; } // funcao que remove um objeto do topo da pilha public Object pop() { // verifica se a pilha esta vazia if (topo < 0) { return null; } else { // obtem o objeto do topo da pilha Object auxiliar = vetorPilha[topo]; // decrementa o topo (desce um item na pilha) topo--; // retorna o elemento do topo da pilha return auxiliar; } } // funcao que verifica quem esta no topo da pilha public Object top() { // verifica se a pilha esta vazia if (topo < 0) { return null; } else { return vetorPilha[topo]; } } // verifica se a pilha esta vazia public boolean isEmpty() { // verifica se o topo aponta para algum indice valido do vetor if (topo == -1) { return true; } else { return false; } } // verifica se a pilha esta cheia public boolean isFull() { // verifica se o topo aponta para o ultimo elemento do vetor if (topo == tamanhoMaximo - 1) { return true; } else { return false; } } } 

Ordenacao.java

 package lista03Pilhas; public class Ordenacao { public static int[] ordenarDecrescente(int v[]) { Pilha minhaPilha = new Pilha(v.length); recebePilha(minhaPilha); int vetor[] = new int[v.length]; for (int i = 0; i < v.length; i++) { vetor[i] = v[i]; minhaPilha.push(vetor[i]); } int u = ((Integer) minhaPilha.pop()).intValue(); return vetor; } public static void recebePilha(Pilha pilha){ } } 

Main.java

 package lista03Pilhas; import java.util.Arrays; import java.util.Scanner; public class Lista03Pilhas { public static void main(String[] args) { int v[] = new int[]{100, 20, 15, 9, 8, 7, 1}; System.out.println("Vetor:" + Arrays.toString(Ordenacao.ordenarDecrescente(v))); int p; int m; Scanner ent = new Scanner(System.in); int[] ordenarDecrescente = Ordenacao.ordenarDecrescente(v); Pilha minhaPilha = new Pilha(v.length); for (int i = 0; i < v.length; i++) { minhaPilha.push(v[i]); Object menor = minhaPilha.pop(); System.out.print(minhaPilha.pop()); } System.out.println(""); for (int i = 0; i < v.length; i++) { System.out.print(v[i] + " "); } } } 
+4
source share
6 answers

How to put in ascending order an array of integers in Java?

Using Arrays.sort() :

 int[] arr = ...; Arrays.sort(arr); 
+6
source

Well, you can always use Arrays.sort() with the corresponding Comparator . The default comparator works in ascending order, so it’s just a matter of using the this method.

+1
source

Java uses Quick sort to sort primitive types and merge sort to sort objects, I think it is useful to use Arrays.sort () to sort arrays.

+1
source

I think you are looking for something like a Bubble algorithm ...

0
source

You can simply use the "Array.sort ()" method of the java.lang class.

0
source

You can use Arrays.sort (int [] a) .

In Java 6, the sorting algorithm used for this method is Customized quicksort adapted from Jon L. Bentley and M. Douglas McIlroy "Engineering a Sort Function", Software-Practice and Experience, Vol. 23 (11), p. 1249-1265 (November 1993).

In Java 7 and 8, the sorting algorithm used for this method is Double-Pivot Quicksort : Vladimir Yaroslavsky, John Bentley, and Joshua Bloch.

0
source

All Articles