Doubt with Bubble Sort

Asked

Viewed 80 times

-3

package AlgoritmosII;

import javax.swing.*;

public class Exercicio //BubbleSort

{

public static void main(String args[])
    {

        int troca, fim, i, aux, k;

        int tamanho = Integer.parseInt(JOptionPane.showInputDialog("informe o tamanho do vetor"));

        int vetorOrdenar[] = new int[tamanho];
        for (i=0;i<=tamanho;i++) {
            vetorOrdenar[i] = Integer.parseInt(JOptionPane.showInputDialog("Informe os " +tamanho+ " valores do vetor:"));
        }
        troca = 1;
        fim = tamanho - 1;
        while(troca==1)
        {
            troca = 0;
            for(i=0; i<fim;i++)
            {
                if (vetorOrdenar[i] > vetorOrdenar[i+1])
                {
                    aux = vetorOrdenar[i];
                    vetorOrdenar[i] = vetorOrdenar[i+1];
                    vetorOrdenar[i+1]=aux;
                    troca = 1;
                }
            }
            fim = fim - 1;
        }
        for (k=0;k<=tamanho;k++){
            JOptionPane.showMessageDialog(null,vetorOrdenar[i]);
            i++;
        }
    }
}

My goal would be to ask the user to say the size of the vector and the values that would be placed on it, after that it would be placed in ascending order, in the end it should show the ordered form of the vector.

1 answer

1

In these two passages

for (i=0;i<=tamanho;i++)

and

for (k=0;k<=tamanho;k++){

you used the comparator <= to limit the value of the variable to the size of the vector. As in Java the vector starts with index 0 the last vector element will have the index tamanho - 1, ex: if the size is 4 the last index is 3, implying that its comparison allows iteration to be applied in a non-existent index.

In the last loop

for (k=0;k<=tamanho;k++){
            JOptionPane.showMessageDialog(null,vetorOrdenar[i]);
            i++;
        }

you iterate on the variable k but uses the variable i as an index whose value has been left as fim - 1 in the previous loop.

import javax.swing.*;

public class Exercicio //BubbleSort

{

public static void main(String args[])
    {

        int troca, fim, i, aux, k;

        int tamanho = Integer.parseInt(JOptionPane.showInputDialog("informe o tamanho do vetor"));

        int vetorOrdenar[] = new int[tamanho];
        for (i=0;i<tamanho;i++) {// Troque do comparador <= para <
            vetorOrdenar[i] = Integer.parseInt(JOptionPane.showInputDialog("Informe os " +tamanho+ " valores do vetor:"));
        }
        troca = 1;
        fim = tamanho - 1;
        while(troca==1)
        {
            troca = 0;
            for(i=0;i<fim;i++)
            {
                if (vetorOrdenar[i] > vetorOrdenar[i+1])
                {
                    aux = vetorOrdenar[i];
                    vetorOrdenar[i] = vetorOrdenar[i+1];
                    vetorOrdenar[i+1]=aux;
                    troca = 1;
                }
            }
            fim = fim - 1;
        }
        for (k=0;k<tamanho;k++){// Troque do comparador <= para <
            JOptionPane.showMessageDialog(null,vetorOrdenar[k]); //use a variável k como índice
        }
    }
}

Browser other questions tagged

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