4

How do I put in ascending order an array of integers in Java? I have class and Pilha.java Ordenacao.Java, but the program is not working.

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] + " ");
        }

    }
}
1
  • 1
    Where is the sorting part in the whole code you posted? Commented Nov 27, 2012 at 16:16

6 Answers 6

6

How do I put in ascending order an array of integers in Java?

By using Arrays.sort():

int[] arr = ...;
Arrays.sort(arr);
Sign up to request clarification or add additional context in comments.

1 Comment

Please link to the latest Javadoc.
1

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

Comments

1

Java uses Quick sort for sorting of primitive types and merge sort for sorting of Objects, I think it is good to use Arrays.sort() to sort arrays.

Comments

0

I thing you are looking for something like the Bubble algorithm...

Comments

0

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

Comments

0

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

In Java 6 the sorting algorithm used for this method is a
Tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "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 a
Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch.

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.