Search code examples
javasortingbubble-sort

Bubble Sort not Sorting One Element


I have two Methods which have to be void methods, they are using a propriety from the elements in the list named Magnitude to sort them using bubble sort from the smallest to largest, the method OnePassBubbleSort orders one element at a time and sortByMagnitudeWithBubbleSort runs the sorting the times needed to get to get the answer the problem is that the quake with magnitude 2.6 does not seem to order properly.

my code is this:

public void onePassBubbleSort(ArrayList<QuakeEntry> quakes, int numSorted) {
    int minIdx = 0;
    for (int i=0 + 1; i< quakes.size()-numSorted; i++) {

        if (quakes.get(i).getMagnitude() < quakes.get(minIdx).getMagnitude()) {
            QuakeEntry qi = quakes.get(i);
            QuakeEntry qmin = quakes.get(minIdx);
            quakes.set(i,qmin);
            quakes.set(minIdx,qi);
            minIdx = i;            
        }

    }

    System.out.println("Printing Quakes after pass " + numSorted );  
    for(QuakeEntry qe: quakes){
        System.out.println(qe.toString());
    }

}

public void sortByMagnitudeWithBubbleSort(ArrayList<QuakeEntry> in) {

   for (int i=0; i< in.size()-1; i++) {
        onePassBubbleSort(in,i);   
    } 
}

the original data is

  • (-23.27, -67.66), mag = 4.80, depth = -175320.00, title = 69km SE of San Pedro de Atacama, Chile
  • (35.68, -118.10), mag = 1.50, depth = -8280.00, title = 27km W of Inyokern, California
  • (36.22, -117.89), mag = 2.60, depth = -1450.00, title = 12km ESE of Olancha, California
  • (36.95, -121.57), mag = 1.00, depth = -8660.00, title = 6km S of Gilroy, California
  • (38.82, -122.77), mag = 1.40, depth = -1300.00, title = 3km W of Cobb, California

and my result output is

  1. (36.95, -121.57), mag = 1.00, depth = -8660.00, title = 6km S of Gilroy, California
  2. (36.22, -117.89), mag = 2.60, depth = -1450.00, title = 12km ESE of Olancha, California
  3. (38.82, -122.77), mag = 1.40, depth = -1300.00, title = 3km W of Cobb, California
  4. (35.68, -118.10), mag = 1.50, depth = -8280.00, title = 27km W of Inyokern, California
  5. (-23.27, -67.66), mag = 4.80, depth = -175320.00, title = 69km SE of San Pedro de Atacama, Chile

my guess is that I have to return the list from the onepassbubblesort, and run that list again, but I that cannot be done with void methods


Solution

  • You sorting method is only moving the minIdx = i; pointer when there is a match, causing it not to line up if the first element is smaller. Effectively, you are only swapping the first element and any other element you encountered when they match.

    A fix for this would be moving the minIdx = i; outside the if statement so it will always get called:

    for (int i=0 + 1; i< quakes.size()-numSorted; i++) {
    
        if (quakes.get(i).getMagnitude() < quakes.get(minIdx).getMagnitude()) {
            QuakeEntry qi = quakes.get(i);
            QuakeEntry qmin = quakes.get(minIdx);
            quakes.set(i,qmin);
            quakes.set(minIdx,qi);        
        }
        minIdx = i;  
    }
    

    Because the way you coded you function to accept a numSorted argument, another solution to the problem would be assigment minIdx to this variable:

    minIdx = numSorted;