[英]Java quickSortRecursive on List
I'm trying to get my quickSortRecursive
method to work, but it isn't passing my test cases.我试图让我的quickSortRecursive
方法工作,但它没有通过我的测试用例。 Note that I'm sorting by zipcode:请注意,我按邮政编码排序:
public class Location implements Comparable<Location> {
private final String zipCode;
private final String city;
private final Double latitude;
private final Double longitude;
private final String state;
public Location(String zipCode, Double latitude, Double longitude, String city, String state) {
this.zipCode = zipCode;
this.city = city;
this.latitude = latitude;
this.longitude = longitude;
this.state = state;
}
public String getCity() {
return this.city;
}
public String getZipCode() {
return this.zipCode;
}
public Double getLatitude() {
return latitude;
}
public Double getLongitude() {
return longitude;
}
public String getState() {
return state;
}
@Override
public int compareTo(Location o) {
int result = 0;
if(Integer.parseInt(this.zipCode) > Integer.parseInt(o.getZipCode())) {
result = -1;
}
if(Integer.parseInt(this.zipCode) < Integer.parseInt(o.getZipCode())) {
result = 1;
}
return result;
}
This is my Location
class followed by my compareTo
method.这是我的Location
类,后面是我的compareTo
方法。
There is also my quickSort
method that isn't working.还有我的quickSort
方法不起作用。 I have an insertionSort
method that works perfectly fine.我有一个insertionSort
方法,它工作得很好。
public static void quickSortRecursive(List<Location> list, int from, int to) {
if (from < to) {
int pivot = from;
int left = from + 1;
int right = to;
Location pivotValue = list.get(pivot);
while (left <= right) {
while (left <= to && pivotValue.compareTo(pivotValue) < left) {
left++;
}
while (right > from && pivotValue.compareTo(pivotValue) > right) {
right--;
}
if (left < right) {
Collections.swap(list, left, right);
}
}
Collections.swap(list, pivot, left - 1);
quickSortRecursive(list, from, right - 1);
quickSortRecursive(list, right + 1, to);
}
}
public static void quickSort(List<Location> locations) {
quickSortRecursive(locations, 0, locations.size() - 1);
}
public static void insertionSort(List<Location> locations) {
for (int j = 1; j < locations.size(); j++) {
Location current = locations.get(j);
int i = j-1;
while ((i > -1) && ((locations.get(i).compareTo(current)) == 1)) {
locations.set(i+1, locations.get(i));
i--;
}
locations.set(i+1, current);
}
}
This should fix the quick sort issue.这应该可以解决快速排序问题。
public void quick_sort(List<Location> locations) {
quick_sort(locations, 0, locations.size() - 1);
}
private void quick_sort(List<Location> locations, int startIndex, int endIndex) {
if (startIndex > endIndex) {
return;
}
int pivotIndex = partition(locations, startIndex, endIndex);
quick_sort(locations, startIndex, pivotIndex - 1);
quick_sort(locations, pivotIndex + 1, endIndex);
}
private int partition(List<Location> locations, int startIndex, int endIndex) {
int smallerIndex = startIndex;
for (int biggerIndex = startIndex + 1; biggerIndex <= endIndex; biggerIndex++) {
if (locations.get(startIndex).compareTo(locations.get(biggerIndex)) == -1) {
smallerIndex++;
swapElements(locations, smallerIndex, biggerIndex);
}
}
swapElements(locations, startIndex, smallerIndex);
return smallerIndex;
}
private void swapElements(List<Location> input, int firstIndex, int secondIndex) {
Location temp = input.get(firstIndex);
input.set(firstIndex, input.get(secondIndex));
input.set(secondIndex, temp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.