简体   繁体   English

气泡排序不排序一个元素

[英]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. 我有两个必须为空方法的方法,它们使用列表中名为Magnitude的元素的专有性,使用从最小到最大的冒泡排序对它们进行排序,方法OnePassBubbleSort一次对一个元素进行排序,sortByMagnitudeWithBubbleSort运行排序获得答案所需的时间问题是2.6级地震似乎没有正确定序。

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 (-23.27,-67.66),mag = 4.80,深度= -175320.00,标题=智利圣佩德罗·德·阿塔卡马的69km SE
  • (35.68, -118.10), mag = 1.50, depth = -8280.00, title = 27km W of Inyokern, California (35.68,-118.10),mag = 1.50,深度= -8280.00,标题= 27km,加利福尼亚Inyokern
  • (36.22, -117.89), mag = 2.60, depth = -1450.00, title = 12km ESE of Olancha, California (36.22,-117.89),mag = 2.60,深度= -1450.00,标题= 12公里,加利福尼亚州奥兰查的ESE
  • (36.95, -121.57), mag = 1.00, depth = -8660.00, title = 6km S of Gilroy, California (36.95,-121.57),mag = 1.00,深度= -8660.00,标题= 6km S of Gilroy,California
  • (38.82, -122.77), mag = 1.40, depth = -1300.00, title = 3km W of Cobb, California (38.82,-122.77),mag = 1.40,深度= -1300.00,标题= 3km W of California,Cobb,California

and my result output is 我的结果输出是

  1. (36.95, -121.57), mag = 1.00, depth = -8660.00, title = 6km S of Gilroy, California (36.95,-121.57),mag = 1.00,深度= -8660.00,标题= 6km S of Gilroy,California
  2. (36.22, -117.89), mag = 2.60, depth = -1450.00, title = 12km ESE of Olancha, California (36.22,-117.89),mag = 2.60,深度= -1450.00,标题= 12公里,加利福尼亚州奥兰查的ESE
  3. (38.82, -122.77), mag = 1.40, depth = -1300.00, title = 3km W of Cobb, California (38.82,-122.77),mag = 1.40,深度= -1300.00,标题= 3km W of California,Cobb,California
  4. (35.68, -118.10), mag = 1.50, depth = -8280.00, title = 27km W of Inyokern, California (35.68,-118.10),mag = 1.50,深度= -8280.00,标题= 27km,加利福尼亚Inyokern
  5. (-23.27, -67.66), mag = 4.80, depth = -175320.00, title = 69km SE of San Pedro de Atacama, Chile (-23.27,-67.66),mag = 4.80,深度= -175320.00,标题=智利圣佩德罗·德·阿塔卡马的69km SE

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 我的猜测是我必须从onepassbubblesort返回列表,然后再次运行该列表,但是我无法使用void方法完成此操作

You sorting method is only moving the minIdx = i; 您的排序方法只是移动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; 解决此问题的方法是移动minIdx = i; outside the if statement so it will always get called: 在if语句之外,因此它将始终被调用:

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: 因为您编写代码的方式是接受numSorted参数,所以解决该问题的另一种方法是将minIdx给此变量:

minIdx = numSorted;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM