英文:
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<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;
//boolean isSorted = true;
for (int i = 0; i<numSorted; i++) {
//isSorted = true;
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);
// isSorted = false;
}
}
loops++;
if (checkInSortedOrder(quakeData)) {
break;
}
// if (isSorted) {
// 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);
}
}
}
答案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 < sParametr) {
stat = true;
}
else {
stat = false;
break;
}
Changing (fParametr < sParametr)
to (fParametr <= sParametr)
should fix the problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论