我试图实现mergesort算法。 合并方法可能是错误的。 到目前为止,这是我的代码:
public static <T extends Comparable<? super T>> void sort(List<T> list) {
mergesort(list, 0, list.size() - 1);
}
private static <T extends Comparable<? super T>> void mergesort(List<T> list, int i, int j) {
if (j - i < 1)
return;
int mid = (i + j) / 2;
mergesort(list, i, mid);
mergesort(list, mid + 1, j);
merge(list, i, mid, j);
}
private static <T extends Comparable<? super T>> void merge(List<T> a, int p, int q, int r) {
int half = q - p + 1;
int otherhalf = r - q;
List<T> left = new ArrayList<T>();
List<T> right = new ArrayList<T>();
for (int i = 0; i < half; i++) {
left.add(i, a.get(p + i));
}
for (int i = 0; i < otherhalf; i++) {
right.add(i, a.get(q + i + 1));
}
int leftindex, rightindex, resultindex;
leftindex = rightindex = resultindex = 0;
while (leftindex < left.size() || rightindex < right.size()) {
if (leftindex < left.size() && rightindex < right.size()) {
if (left.get(leftindex).compareTo(right.get(rightindex)) < 0) {
a.set(resultindex, left.get(leftindex));
resultindex++;
leftindex++;
} else {
a.set(resultindex, right.get(rightindex));
resultindex++;
rightindex++;
}
} else if (leftindex < left.size()) {
a.set(resultindex, left.get(leftindex));
resultindex++;
leftindex++;
} else if (rightindex < right.size()) {
a.set(resultindex, right.get(rightindex));
resultindex++;
rightindex++;
}
}
}
我测试了它。
输入[5, 6, 1, 4]
。
输出为:[1, 1, 4, 4]
:
似乎在合并方法中我没有到达位置5和6。 所以我认为,我必须增加一些东西,但是我不知道在哪里? 有人可以帮我吗?
答案 0 :(得分:2)
问题是resultindex
初始化为0
而不是p
。
以下是其他一些注意事项:
+ 1
的调整可能导致一个错误。if (left.get(leftindex).compareTo(right.get(rightindex)) <= 0)
而不是<
,以确保排序稳定。else if (rightindex < right.size())
是多余的。这是更正的版本:
public static <T extends Comparable<? super T>> void sort(List<T> list) {
mergesort(list, 0, list.size());
}
private static <T extends Comparable<? super T>> void mergesort(List<T> list, int i, int j) {
if (j - i < 2)
return;
int mid = (i + j) / 2;
mergesort(list, i, mid);
mergesort(list, mid, j);
merge(list, i, mid, j);
}
private static <T extends Comparable<? super T>> void merge(List<T> a, int p, int q, int r) {
int leftLen = q - p;
int rightLen = r - q;
List<T> left = new ArrayList<T>();
List<T> right = new ArrayList<T>();
for (int i = 0; i < leftLen; i++) {
left.add(i, a.get(p + i));
}
for (int i = 0; i < rightLen; i++) {
right.add(i, a.get(q + i));
}
int leftIndex = 0;
int rightIndex = 0;
int resultIndex = p;
while (leftIndex < leftLen && rightIndex < rightLen) {
if (left.get(leftIndex).compareTo(right.get(rightIndex)) <= 0) {
a.set(resultIndex, left.get(leftIndex));
resultIndex++;
leftIndex++;
} else {
a.set(resultIndex, right.get(rightIndex));
resultIndex++;
rightIndex++;
}
}
while (leftIndex < leftLen) {
a.set(resultIndex, left.get(leftIndex));
resultIndex++;
leftIndex++;
}
while (rightIndex < rightLen) {
a.set(resultIndex, right.get(rightIndex));
resultIndex++;
rightIndex++;
}
}
此外,由于右边的元素在被复制之前不会被覆盖,因此无需保存右边的一半。确实,复制其余的右半部分是没有用的,因为这些元素已经存在。
以下是简化版本:
private static <T extends Comparable<? super T>> void merge(List<T> a, int p, int q, int r) {
int half = q - p;
List<T> left = new ArrayList<T>();
for (int i = 0; i < half; i++) {
left.add(i, a.get(p + i));
}
int leftIndex = 0;
int rightIndex = q;
int resultIndex = p;
while (leftIndex < half && rightIndex < r) {
if (left.get(leftIndex).compareTo(a.get(rightIndex)) <= 0) {
a.set(resultIndex, left.get(leftIndex));
resultIndex++;
leftIndex++;
} else {
a.set(resultIndex, a.get(rightIndex));
resultIndex++;
rightIndex++;
}
}
while (leftIndex < half) {
a.set(resultIndex, left.get(leftIndex));
resultIndex++;
leftIndex++;
}
}
进一步简化代码:
List
数组不需要动态left
,以下是简化但可能不太易读的代码:
private static <T extends Comparable<? super T>> void merge(List<T> a, int p, int q, int r) {
int i, half = q - p;
T[] left = new T[half];
for (i = 0; i < half; i++)
left[i] = a.get(p + i);
for (i = 0; i < half; ) {
if (q == r || left[i].compareTo(a.get(q)) <= 0)
a.set(p++, left[i++]);
else
a.set(p++, a.get(q++));
}
}