所以我尽力优化我的Quicksort算法以尽可能高效地运行,即使对于已排序或接近排序的数组,使用三个值的中间值的枢轴,以及对小分区大小使用插入排序。我已经测试了我的代码用于大型随机值数组并且它可以工作,但是当我传递已经排序的数组时,我得到了一个堆栈溢出错误(具有讽刺意味的是它导致我找到了这个网站)。我认为这是我的递归调用的一个问题(我知道分区至少适用于其他数据集),但我不知道要改变什么。
这是我第一学期数据结构课程的一部分,因此任何代码审查也会有所帮助。感谢。
public void quickSort(ArrayList<String> data, int firstIndex, int numberToSort) {
if (firstIndex < (firstIndex + numberToSort - 1))
if (numberToSort < 16) {
insertionSort(data, firstIndex, numberToSort);
} else {
int pivot = partition(data, firstIndex, numberToSort);
int leftSegmentSize = pivot - firstIndex;
int rightSegmentSize = numberToSort - leftSegmentSize - 1;
quickSort(data, firstIndex, leftSegmentSize);
quickSort(data, pivot + 1, rightSegmentSize);
}
}
public int partition(ArrayList<String> data, int firstIndex, int numberToPartition) {
int tooBigNdx = firstIndex + 1;
int tooSmallNdx = firstIndex + numberToPartition - 1;
String string1 = data.get(firstIndex);
String string2 = data.get((firstIndex + (numberToPartition - 1)) / 2);
String string3 = data.get(firstIndex + numberToPartition - 1);
ArrayList<String> randomStrings = new ArrayList<String>();
randomStrings.add(string1);
randomStrings.add(string2);
randomStrings.add(string3);
Collections.sort(randomStrings);
String pivot = randomStrings.get(1);
if (pivot == string2) {
Collections.swap(data, firstIndex, (firstIndex + (numberToPartition - 1)) / 2);
}
if (pivot == string3) {
Collections.swap(data, firstIndex, firstIndex + numberToPartition - 1);
}
while (tooBigNdx < tooSmallNdx) {
while ((tooBigNdx < tooSmallNdx) && (data.get(tooBigNdx).compareTo(pivot) <= 0)) {
tooBigNdx++;
}
while ((tooSmallNdx > firstIndex) && (data.get(tooSmallNdx).compareTo(pivot) > 0)) {
tooSmallNdx--;
}
if (tooBigNdx < tooSmallNdx) {// swap
Collections.swap(data, tooSmallNdx, tooBigNdx);
}
}
if (pivot.compareTo(data.get(tooSmallNdx)) >= 0) {
Collections.swap(data, firstIndex, tooSmallNdx);
return tooSmallNdx;
} else {
return firstIndex;
}
}
答案 0 :(得分:2)
您可以避免堆栈溢出而不会过多地更改算法。诀窍是在最大的分区上进行尾部调用优化,并且只在最小的分区上使用递归。这通常意味着您必须将if
更改为while
。我现在无法真正测试java代码,但它应该看起来像:
public void quickSort(ArrayList<String> data, int firstIndex, int numberToSort) {
while (firstIndex < (firstIndex + numberToSort - 1))
if (numberToSort < 16) {
insertionSort(data, firstIndex, numberToSort);
} else {
int pivot = partition(data, firstIndex, numberToSort);
int leftSegmentSize = pivot - firstIndex;
int rightSegmentSize = numberToSort - leftSegmentSize - 1;
//only use recursion for the smallest partition
if (leftSegmentSize < rightSegmentSize) {
quickSort(data, firstIndex, leftSegmentSize);
firstIndex = pivot + 1;
numberToSort = rightSegmentSize;
} else {
quickSort(data, pivot + 1, rightSegmentSize);
numberToSort = leftSegmentSize;
}
}
}
这可确保调用堆栈大小最多为O(log n)
,因为在每次调用时,您只对最多n/2
大小的数组使用递归。
答案 1 :(得分:1)
在partition
方法中,您有时会使用范围之外的元素:
String string1 = data.get(firstIndex);
String string2 = data.get((firstIndex + (numberToPartition - 1)) / 2);
String string3 = data.get(firstIndex + numberToPartition - 1);
(firstIndex + (numberToPartition - 1)) / 2
不是中间元素的索引。那将是(firstIndex + (firstIndex + (numberToPartition - 1))) / 2
= firstIndex + ((numberToPartition - 1) / 2)
。
实际上,如果firstIndex > n/2
(其中n
是输入中元素的数量),则使用索引小于firstIndex
的元素。对于排序数组,这意味着您选择firstIndex
处的元素作为pivot元素。因此,您可以在
,
导致堆栈溢出以获得足够大的输入。