Logical Problem Ordering Method BubbleSort

-3

I am doing sort numbers from a CSV file. I import the numbers and step into an Array List and convert the ArrayList<String> to ArrayList<Integer> . It happens that I am having a logic problem when sorting the numbers. The numbers have even been 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:

Methodtopassthenumbersfromthe.CSVfiletotheArrayList:

publicvoidPassVector(){StringLineFile=newString();FilefileCSV=newFile(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 ArrayList <String> to 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;
}   

Ordering Method BubbleSort (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 sorted

    
asked by anonymous 20.11.2018 / 08:26

1 answer

3

The logic of BubbleSort works by comparing an X value set by the outermost loop with all others in the array through the loop more internal, and when this value X is smaller, the position change is made.

None of your ties follow this logic, bubblesort sweeps one by one. The logic of this method would be as follows:

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
      }

    }
}

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

    
20.11.2018 / 11:15