Merge Sorted Java

Asked

Viewed 96 times

0

I need to merge in java, with values of an Instance of a class, Queue a and Queue b, I made a method to insert(), in the Queue class. and would like to access the values of a and b. And assign these values to another Instance of the Queue class, type Queue c = new Queue (gets the values of A and B). But I don’t have much idea to do that, I appreciate any kind of help.

Class Queue with other methods

public class Fila {

    private int remove;
    private int max = 0;
    public int dado[];

    public Fila(int tamanhoFila) {
        this.dado = new int[tamanhoFila];
        this.remove = 0;
    }

    Fila() {
        this(3);
    }

    public  void insere(int elemento) {
        if (this.max < this.dado.length) {
            this.dado[this.max] = elemento;
            this.max++;
        }
    }

    public void cheia() {
        if (this.max == this.dado.length) {
            System.out.println("A Fila esta cheia");
        } else {
            System.out.println("A Fila não esta cheia");
        }
    }

    public boolean vazia() {
        if (this.max == 0) {
            return true;
        }
        return false;
    }

    public Object primeiro() {
        if (this.vazia()) {
            return "A fila esta vazia !";
        }

        return this.dado[0];
    }

    public Object ultimo() {
        return this.dado[--max];
    }

    public Object remove() {
        int pos = 0;
        int removeElemento = this.dado[pos];

        for (int i = pos; i < this.max - 1; i++) {
            dado[i] = dado[i + 1];
        }
        this.max--;

        return removeElemento;

    }



    @Override
    public String toString() {
        return Arrays.toString(this.dado);
    }

}

Class Executes with the main method

public class Executa {

    public static void main(String[] args) {
        Fila a = new Fila(4);
        Fila b = new Fila(5);
        Fila c = new Fila(9);

        a.insere(12);
        a.insere(35);
        a.insere(52);
        a.insere(64);


        b.insere(05);
        b.insere(15);
        b.insere(23);
        b.insere(55);
        b.insere(75);



    }

}

2 answers

0

Add this constructor with these two additional parameters, fila1 and fila2, row 1 will be the row that will have its first value (from dado) at the beginning of dado of the queue being instantiated:

Fila(int tamanhoFila, Fila fila1, Fila fila2) {
    this.dado = new int[tamanhoFila];
    this.remove = 0;

    int i = 0, j = 0; // Guarda a posição atual em fila1.dado e fila2.dado
    // Repete até não sobrar valores a ler nos dois
    while (i != fila1.dado.length || j != fila2.dado.length) {
        // O valor de fila1.dado[i] é o primeiro a ser adicionado
        if (i < fila1.dado.length) {
            this.dado[i+j] = fila1.dado[i];
            i++;
        }
        // O valor de fila2.dado[j] é adicionado depois
        if (j < fila2.dado.length) {
            this.dado[i+j] = fila2.dado[j];
            j++;
        }
    }
}

With these parameters, your example requires that the Fila c is instantiated in this way:

Fila c = new Fila(9, b, a);
  • I made the constructor method, as reported above. Only when instantiating in the Run class, it presents me with errors in the instance. Displaying errors in b and a. public class Executes { public Static void main(String[] args) { Fila c = new Fila(9, b, a); } }

  • See if you urged c only after entering the values of the other two Filas, if you still have the problem edit your question showing what you tried and where and which errors are.

  • I managed to, thank you very much.

  • If you’ve got it differently, answer your question with your solution and mark it as correct, to help other people with a similar question.

0

I used a method for such a solution

public Static Merge queue(Queue queueA, Queue queue) { Row row row = new Row(row.dado.length + row.dado.length);

    int proximoA = 0;
    int proximoB = 0;
    int proximoC = 0;

    while (proximoA < filaA.dado.length && proximoB < filaB.dado.length) {
        if (filaA.dado[proximoA] <= filaB.dado[proximoB]) {
            filaC.dado[proximoC++] = filaA.dado[proximoA++];
        } else {
            filaC.dado[proximoC++] = filaB.dado[proximoB++];
        }
    }

    while (proximoA < filaA.dado.length) {
        filaC.dado[proximoC++] = filaA.dado[proximoA++];
    }

    while (proximoB < filaB.dado.length) {
        filaC.dado[proximoC++] = filaB.dado[proximoB++];
    }

    return filaC;

}

and in the main method, the code went like this

public class Executes {

public static void main(String[] args) {

    Fila filaA = new Fila(4);
    Fila filaB = new Fila(5);

    filaA.insere(2);
    filaA.insere(4);
    filaA.insere(5);
    filaA.insere(6);

    filaB.insere(9);
    filaB.insere(1);
    filaB.insere(10);
    filaB.insere(3);
    filaB.insere(12);

    Fila filaMergeada = Fila.merge(filaA, filaB);

    filaMergeada.mostrarElementosDaFila();

}

}

Browser other questions tagged

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