我的检查算法无法停止冒泡排序。

huangapple go评论55阅读模式
英文:

My check algorithm doesn't stop bubble sort

问题

import java.util.*;
import edu.duke.*;

public class QuakeSortInPlace {
       
    public boolean checkInSortedOrder(ArrayList<QuakeEntry> quakes) {
        boolean stat = true;
        for (int i = 0; i < quakes.size() - 1; i++) {
            double fParametr = quakes.get(i).getMagnitude();
            double sParametr = quakes.get(i + 1).getMagnitude();
            if (fParametr < sParametr) {
                stat = true;
            } else {
                stat = false;
                break;
            }
        }
        return stat;
    }
    
    public void onePassBubbleSort(ArrayList<QuakeEntry> quakeData, int numSorted) {
        int loops = 0;
       
        for (int i = 0; i < numSorted; i++) {
            for (int k = 0; k < quakeData.size() - i - 1; k++) {
                double firstCompElement = quakeData.get(k).getMagnitude();
                double secondCompElement = quakeData.get(k + 1).getMagnitude();
                if (firstCompElement > secondCompElement) {
                    QuakeEntry first = quakeData.get(k);
                    QuakeEntry second = quakeData.get(k + 1);
                    quakeData.set(k + 1, first);
                    quakeData.set(k, second);
                }
            }
            loops++;
            
            if (checkInSortedOrder(quakeData)) {
                break;
            }
        }
        System.out.print("We needed: " + loops);
    }
       
    public void sortByMagnitudeWithBubbleSortWithCheck(ArrayList<QuakeEntry> in) {
        int numOfElements = in.size();
        int finalNumOfElements = numOfElements - 1;
        onePassBubbleSort(in, finalNumOfElements);
    }
    
    public void testSort() {
        EarthQuakeParser parser = new EarthQuakeParser(); 
        String source = "data/nov20quakedatasmall.atom";
        ArrayList<QuakeEntry> list = parser.read(source); 
        System.out.println("read data for " + list.size() + " quakes");    
        sortByMagnitudeWithBubbleSortWithCheck(list);
        for (QuakeEntry qe : list) { 
             System.out.println(qe);
        } 
    }
}

Note: This is the provided code with the requested translation. The code is related to sorting earthquake data using the bubble sort algorithm. If you have any questions or need further assistance, feel free to ask.

英文:

I have one file with 25 values of magnitude. I've checked that my bubble sort algorithm managed for 19 or 20 loops. My check method doesn't stop algorithm, It writes 24 loop needed. I can't understand, what I do wrong. It is zip file, where you can find data folder and nov20quakedatasmall.atom fid to test

import java.util.*;
import edu.duke.*;

public class QuakeSortInPlace {

public boolean checkInSortedOrder (ArrayList&lt;QuakeEntry&gt; quakes) {
boolean stat = true;
for (int i = 0; i&lt; quakes.size()-1; i++) {
double fParametr = quakes.get(i).getMagnitude();
double sParametr = quakes.get(i+1).getMagnitude();
if (fParametr &lt; sParametr) {
stat = true;
}
else {
stat = false;
break;
}
}
return stat;
}
public void onePassBubbleSort (ArrayList &lt;QuakeEntry&gt; quakeData, int numSorted) {
int loops = 0;
//boolean isSorted = true;
for (int i = 0; i&lt;numSorted; i++) {
//isSorted = true;
for (int k = 0; k&lt;quakeData.size()-i-1; k++) {
double firstCompElement = quakeData.get(k).getMagnitude();
double secondCompElement = quakeData.get(k+1).getMagnitude();
if (firstCompElement &gt; secondCompElement) {
QuakeEntry first = quakeData.get(k);
QuakeEntry second = quakeData.get(k+1);
quakeData.set(k+1 ,first);
quakeData.set(k,second);
// isSorted = false;
}
}
loops++;
if (checkInSortedOrder(quakeData)) {
break;
}
// if (isSorted) {
// break;
// }          
}
System.out.print(&quot;We needed: &quot;+loops);

}

public void sortByMagnitudeWithBubbleSortWithCheck (ArrayList&lt;QuakeEntry&gt; in) {
int numOfElements = in.size();
int finalNumOfElements = numOfElements-1;
onePassBubbleSort (in, finalNumOfElements);
}
public void testSort() {
EarthQuakeParser parser = new EarthQuakeParser(); 
String source = &quot;data/nov20quakedatasmall.atom&quot;;
ArrayList&lt;QuakeEntry&gt; list  = parser.read(source); 
System.out.println(&quot;read data for &quot;+list.size()+&quot; quakes&quot;);    
sortByMagnitudeWithBubbleSortWithCheck(list);
for (QuakeEntry qe: list) { 
System.out.println(qe);
} 
}

}

答案1

得分: 1

在您的源文件中有两个具有相同值的条目。这会触发您的if语句中的“else”,并导致程序继续进行下一次循环。

if (fParametr < sParametr) {
    stat = true;
}
else {
    stat = false;
    break;
}

(fParametr < sParametr) 更改为 (fParametr <= sParametr) 应该可以解决这个问题。

英文:

There are two entries in your source file that have the same value. This triggers the "else" in your if-statement and causes the program to continue with the next loop.

if (fParametr &lt; sParametr) {
stat = true;
}
else {
stat = false;
break;
} 

Changing (fParametr &lt; sParametr) to (fParametr &lt;= sParametr) should fix the problem.

huangapple
  • 本文由 发表于 2020年9月16日 06:32:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63910695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定