问题
I have a data file that I need to sort the information by Make&&Model of vehicle. My bubblesort doesn't work, can you help me to fix my problem, please ? Thank you very much! P.S. I can't have extra methods :( if I remove getMake() method, it works but && getModel doesn't work at all :(
public static void sortByVehicleMakeModel(Vehicle[] vehicles) {
for(int y = 0; y < vehicles.length; y++) {
for (int x = 0 ; x < vehicles.length - 1 ; x++){
if((vehicles[x].getMake().compareToIgnoreCase(vehicles[x+1].getMake()) > 0) && (vehicles[x].getModel().compareToIgnoreCase(vehicles[x+1].getModel()) > 0)) {
swap(vehicles, x, x + 1);
}
}
}
for(int x = 0; x < vehicles.length - 1; x++){
System.out.println(vehicles[x].getMake());
}
}
回答1:
Follow these steps to compare two vehicles:
- compare the make of the two vehicles
- if they're not equal, return the result of the comparison
- if they are equal, compare the model and return the result
You should replace the code in your if statement with a method that compares two Vehicles using the logic described above.
Something like this:
if (compareVehicles(vehicles[x], vehicles[x + 1]) > 0) {
swap(vehicles, x, x + 1);
}
To do this the correct way, you should make Vehicle implement Comparable.
That way you can put the above logic in your compareTo method.
This will allow you to simply do this:
if (vehicles[x].compareTo(vehicles[x + 1]) > 0) {
swap(vehicles, x, x + 1);
}
Here's a simple example of how to implement Comparable:
class Vehicle implements Comparable<Vehicle> {
private String make;
private String model;
public int compareTo(Vehicle other) {
if (other == null) {
return -1;
}
int compareVal = make.compareToIgnoreCase(other.make);
if (compareVal == 0) {
return model.compareToIgnoreCase(other.model);
}
else {
return compareVal;
}
}
}
Ok... since it's been a few days, I'll just show you how to do it.
public static void sortVehicles(Vehicle[] vehicles) {
for (int i = 0; i < vehicles.length - 1; i++) {
Vehicle curr = vehicles[i];
Vehicle next = vehicles[i + 1];
String currMake = curr.getMake();
String nextMake = next.getMake();
int compareVal = currMake.compareToIgnoreCase(nextMake);
// if the makes are the same, we need to compare the models
if (compareVal == 0) {
String currModel = curr.getModel();
String nextModel = next.getModel();
compareVal = currModel.compareToIgnoreCase(nextModel);
}
if (compareVal > 0) {
swap(vehicles, i, i + 1);
}
}
for (Vehicle v : vehicles) {
System.out.println(v.getMake());
}
}
回答2:
Just to improve the performance(I realize for the comparison logic, @jahroy is right). I think the code of the second loop should be as below in your case: x < vehicles.length - y -1
for(int y = 0; y < vehicles.length; y++) {
for (int x = 0 ; x < vehicles.length - y -1 ; x++){
if((vehicles[x].getMake().compareToIgnoreCase(vehicles[x+1].getMake()) > 0) && (vehicles[x].getModel().compareToIgnoreCase(vehicles[x+1].getModel()) > 0)) {
swap(vehicles, x, x + 1);
}
}
}
回答3:
here is how i fixed it:
public static void sortByVehicleMakeModel(Vehicle[] vehicles) {
for(int y = 0; y < vehicles.length; y++) {
for (int x = 0 ; x < vehicles.length - 1 ; x++){
boolean compare1 = (vehicles[x].getMake().compareToIgnoreCase(vehicles[x+1].getMake()) > 0);
if (compare1){
swap(vehicles, x, x + 1);
}
}
}
来源:https://stackoverflow.com/questions/13576577/bubblesort-array-ascending