[英]In-place quicksort woes
I've been struggling with this for too many hours now. 我已经为此苦苦挣扎了太多小时了。 I'm trying to build an in-place quicksort in Java following a pseudocode example .
我正在尝试根据伪代码示例在Java中构建就地快速排序。 Yes, I've tried implemented the example verbatim, but it's got an infinite loop problem.
是的,我尝试逐字实现示例,但是它存在无限循环问题。 But anyway the logic seems sound, so I've been wrestling with fixing it for hours.
但是无论如何,逻辑似乎是合理的,所以我一直在努力解决它几个小时。
I can't for the life of me get the darn thing to work - here's an SSCCE using a trivial list: 15, 12, 13
. 我不能为我的生命得到混账东西的工作-这里是使用小型列表中SSCCE:
15, 12, 13
。 Things sort when I expect them, but then the sub-lists are not split correctly. 当我期望它们排序时,事情就排序了,但是子列表却没有正确拆分。 If I remove pointers in the swap area, duplicates are not supported.
如果删除交换区域中的指针,则不支持重复项。 Can someone help out, or point me to a similar implementation that works (for which I've looked unsuccessfully)?
有人可以帮忙吗,或为我指出一个可行的类似实现(对于该实现,我看失败了)?
SSCCE, no traces: SSCCE,无痕迹:
import java.util.Arrays;
import java.util.List;
public class Quicksort {
public static void main(final String[] args0) {
List<Integer> unsorted = Arrays.asList(new Integer[]{15, 12, 13});
int size = unsorted.size();
int left = 0;
int right = size - 1;
int pivot = 13; // Median of first, middle, last. Method removed for brevity.
while (left < right) {
while (unsorted.get(left).compareTo(pivot) < 0) {
left++;
}
while (unsorted.get(right).compareTo(pivot) > 0) {
right--;
}
if (unsorted.get(left).compareTo(unsorted.get(right)) > 0) {
int old = unsorted.get(left);
unsorted.set(left, unsorted.get(right));
unsorted.set(right, old);
if (left + 1 < size) {
left++;
}
if (right - 1 >= 0) {
right--;
}
}
}
System.out.println("List: " + unsorted);
System.out.println("Sublist left: " + unsorted.subList(0, left)); // Yields [13, 15]
System.out.println("Sublist right: " + unsorted.subList(left, size)); // Yields [12]
// ...then recurse for sublists.
}
}
SSCCE, with traces (same as above): SSCCE,带有痕迹(与上面相同):
import java.util.Arrays;
import java.util.List;
public class Quicksort {
public static void main(final String[] args0) {
List<Integer> unsorted = Arrays.asList(new Integer[]{15, 12, 13});
int size = unsorted.size();
int left = 0;
int right = size - 1;
int pivot = 13; // Median of first, middle, last. Method removed for brevity.
System.out.println("---Processing " + unsorted);
System.out.println("Median " + pivot);
System.out.println("Left = " + left + ", right = " + right);
while (left < right) {
System.out.println("Start INC/DEC, left = " + left + ", right = " + right);
while (unsorted.get(left).compareTo(pivot) < 0) {
left++;
}
while (unsorted.get(right).compareTo(pivot) > 0) {
right--;
}
System.out.println("End INC/DEC, left = " + left + ", right = " + right);
if (unsorted.get(left).compareTo(unsorted.get(right)) > 0) {
int old = unsorted.get(left);
unsorted.set(left, unsorted.get(right));
unsorted.set(right, old);
if (left + 1 < size) {
left++;
}
if (right - 1 >= 0) {
right--;
}
System.out.println("End swap, left = " + left + ", right = " + right + ", list now " + unsorted);
}
System.out.println("End while, left = " + left + ", right = " + right + ", list now " + unsorted);
}
System.out.println("List: " + unsorted);
System.out.println("Sublist left: " + unsorted.subList(0, left));
System.out.println("Sublist right: " + unsorted.subList(left, size));
// ...then recurse for sublists.
}
}
It seems that you compare values before swapping: 似乎您在交换之前比较了值:
if (unsorted.get(left).compareTo(unsorted.get(right)) > 0)
while the reference code compares indices: 而参考代码比较索引:
if left ≤ right
Also see the difference in comparison: <=
instead of >
. 另请参见比较中的区别:
<=
代替>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.