Logical Problem Bubblesort Sorting Method

Asked

Viewed 123 times

-3

I am sorting numbers from a CSV file. I import the numbers and step into an Array List and convert the ArrayList<String> for ArrayList<Integer>. It turns out I’m having a little logic problem sorting the numbers. The numbers have even organized, but the largest number is coming out in front of everyone, and only from the next number that starts the ascending order, as below:

inserir a descrição da imagem aqui

Method to pass file numbers . CSV to Arraylist:

public void PassVector()
{


    String LineFile = new String ();

    File fileCSV = new File(LocalFile.getText());

    try 

    {

        Numb = (ArrayList<String>) Files.lines(Paths.get(LocalFile.getText()))
        .flatMap(Pattern.compile(",")::splitAsStream)
        .collect(Collectors.toList());

        System.out.println(Numb);



        Scanner reader = new Scanner(fileCSV);

        while (reader.hasNext())

        {

            LineFile = reader.nextLine();
            NumbLines+=1;

        }

        NumbNumbers = NumbLines * 2;

        //System.out.println(NumbLines);

        //ConvertArrayList(Numb);



    }           


    catch (IOException e)

    {
        System.out.println("Não foi possível carregar o vetor. \n\n Código do erro: " + e);
    }


}

Method to convert the ArrayList <String> for ArrayList<Integer>

public static ArrayList<Integer> getIntegerArray(ArrayList<String> ArrayConvert) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for(String stringValue : ArrayConvert) {
        try {
            //Convert String to Integer, and store it into integer array list.
            result.add(Integer.parseInt(stringValue));
        } catch(NumberFormatException nfe) {
           //System.out.println("Could not parse " + nfe);
            //Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
        } 
    }       
    return result;
}   

Bubblesort Sorting Method (With logical error):

public static void Bubble(ArrayList<Integer> BubbleVector)

    {



        for (int i=0; i<BubbleVector.size();i=i+2)

        {

            for (int j=2; j<(BubbleVector.size()-1);j=j+2)

            {

                if (BubbleVector.get(i) < BubbleVector.get(j))

                {
                    Auxiliar1 = BubbleVector.get(i);
                    Auxiliar2 = BubbleVector.get(i+1);
                    BubbleVector.set(i, BubbleVector.get(j));
                    BubbleVector.set(i+1, BubbleVector.get(j+1));
                    BubbleVector.set(j, Auxiliar1);
                    BubbleVector.set(j+1, Auxiliar2);

                }

            }


        }

        System.out.println(BubbleVector);

    }

CSV file with numbers to be ordered

Arquivo CSV com números a serem ordenados

  • Collections.sort(BubbleVector) Simple as that.

1 answer

3

The logic of Bubblesort works by comparing an X value fixed by the outermost loop with all others of the array through the innermost loop, and when this X value is smaller, the position change is made.

None of its loops follow this logic, bubblesort sweeps from one to one. The logic of this method would be the following:

int size = lista.size();

for(int i = 0; i < size; i++) {

    for(int j = i; j < size; j++) {

      if(array.get(i) < array.get(j)){
       //troca
      }

    }
}

Recalling that Classes derived from Collections can only be ordered with a single Collections.sort(lista)

Browser other questions tagged

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