【发布时间】:2020-08-14 03:27:24
【问题描述】:
我一直在比较递归快速排序和迭代快速排序的性能,似乎我的递归快速排序始终比我的迭代版本快。我只是想知道递归版本是否有任何理由会更快?据我了解,迭代版本应该执行得更快,因为它避免了递归调用的成本。
递归快速排序实现
int Pivot = 1;
QuickSort cleanRun = new QuickSort(runArray, Pivot);
int last = runArray.length - 1;
long start = System.nanoTime();
cleanRun.quickSort(0, last);
long end = System.nanoTime();
递归快速排序类
public class QuickSort extends SortingAlgorithm {
public QuickSort(int[] l, int pivot) {
super(l);
}
public int[] getL() {
return l;
}
public void quickSort(int first, int last) {
int splitPoint;
if (first < last) {
splitPoint = split(first, last);
quickSort(first, splitPoint - 1);
quickSort(splitPoint + 1, last);
}
}
private int split(int first, int last) {
int splitPoint, unknown;
int x;
int temp;
int temp2;
x = l[first];
splitPoint = first;
for (unknown = first + 1; unknown <= last; unknown++) {
if (l[unknown] < x) {
splitPoint = splitPoint + 1;
//interchange(splitPoint, unknown);
temp = l[splitPoint];
l[splitPoint] = l[unknown];
l[unknown] = temp;
}
}
temp = l[first];
l[first] = l[splitPoint];
l[splitPoint] = temp;
return splitPoint;
}
}
迭代快速排序实现
QuickSortStack cleanRun = new QuickSortStack(runArray);
int last = runArray.length - 1;
long start = System.nanoTime();
cleanRun.explicitStackingQuicksortCustomPivot(runArray);
long end = System.nanoTime();
迭代快速排序类
public class QuickSortStack extends SortingAlgorithm {
public QuickSortStack(int[] l) {
super(l);
}
public int[] getL() {
return l;
}
/**
*
* @param l
*/
public void explicitStackingQuicksortCustomPivot(int l[]){
//these are the indexes
int first, last, splitPoint;
Stack stack = new Stack();
stack.push(0);
last = l.length-1;
stack.push(last);
while(!stack.empty())
{
last = (int) stack.pop();
first = (int) stack.pop();
while(first < last){
splitPoint = split2(first, last);
stack.push (splitPoint+1);
stack.push(last);
last = splitPoint - 1;
}
}
}
/**
*
* @param first
* @param last
* @return
*/
private int split2(int first, int last) {
int splitPoint, unknown;
int x;
int temp;
x = l[first];
splitPoint = first;
for (unknown = first + 1; unknown <= last; unknown++) {
if (l[unknown] < x) {
splitPoint = splitPoint + 1;
//interchange(splitPoint, unknown);
temp = l[splitPoint];
l[splitPoint] = l[unknown];
l[unknown] = temp;
}
}
temp = l[first];
l[first] = l[splitPoint];
l[splitPoint] = temp;
return splitPoint;
}
}
【问题讨论】:
-
这看起来可能是 Java 基准测试的事情,而不是快速排序的事情;参见例如stackoverflow.com/q/504103/869736
-
Stack又旧又慢。尝试改用ArrayDeque -
这些都不是快速排序。更像是递归冒泡排序或壳排序。比较下面的答案,这是一个快速排序。完全不同。
-
您的测量方式有缺陷。您主要是在测量副作用,而不是您的实际代码。这使您的基准测试几乎毫无用处。请阅读如何正确使用 Java 进行微基准测试(使用 JMH)。
标签: java sorting recursion quicksort