【发布时间】:2017-03-13 00:37:34
【问题描述】:
我尝试实现Introsort,它以quicksort 算法开始,但一旦递归深度达到2 * log(array length),就会转换为heapsort。
我试图在一个小数组上运行它(数组大小 = 20) 并计算并打印它使用堆或快速排序的次数。问题是每次我运行它时,我都会得到不同的统计数据。 E.G 第一次运行它使用了 6 次快速排序和 6 次堆排序,下一次可能是 11 次和 14 次。这是为什么?
这是我的测试课:
public class TestClass {
public static void main(String[] args) {
int[] myArray = randomIntArray();
IntroSort.sort(myArray);
}
private static int[] randomIntArray() {
Random rand = new Random();
int[] newIntArray = new int[20];
for(int i = 0; i < newIntArray.length; i++){
newIntArray[i] = rand.nextInt((1000 - 0) + 1) + 0;
}
return newIntArray;
}
}
这是我的介绍排序课程:
public class IntroSort {
private static int quick = 0;
private static int heap = 0;
/*
* ------------------------------------------------------
* Interface to the outer world, takes an array as
* parameter, and calculates the max depth allowed.
* ------------------------------------------------------
*/
public static void sort(int[] arrayToSort){
int depth = ((int) Math.log(arrayToSort.length))*2;
sort(arrayToSort, depth, 0, arrayToSort.length-1);
System.out.println("Total QuickSorts: "+quick);
System.out.println("Total HeapSorts: "+heap);
}
/*
* ------------------------------------------------------
* Sorting loop, decides whether to use quicksort or
* heapsort.
* ------------------------------------------------------
*/
private static void sort(int[] arrayToSort, int depth, int start, int end){
int length = arrayToSort.length;
if(length <= 1){
return;
}else if(depth == 0){
heap++;
heapSort(arrayToSort, start, end);
}else{
if(start >= end)
return;
quick++;
int pivot = arrayToSort[(start + end)/2];
int index = partition(arrayToSort, start, end, pivot);
sort(arrayToSort, depth-1, start, index-1);
sort(arrayToSort, depth-1, index, end);
}
}
/*
* ------------------------------------------------------
* Heap sort implementation, taken and modified from
* HeapSort.java
* ------------------------------------------------------
*/
private static void heapSort(int[] arrayToSort, int start, int end){
for (int i = end / 2 - 1; i >= start; i--)
heapify(arrayToSort, end, i);
for (int i=end-1; i>=start; i--){
int temp = arrayToSort[start];
arrayToSort[start] = arrayToSort[i];
arrayToSort[i] = temp;
heapify(arrayToSort, i, start);
}
}
/*
* ------------------------------------------------------
* Heapify implementation, taken and modified from
* HeapSort.java
* ------------------------------------------------------
*/
private static void heapify(int[] arrayToSort, int n, int i){
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && arrayToSort[l] > arrayToSort[largest])
largest = l;
if (r < n && arrayToSort[r] > arrayToSort[largest])
largest = r;
if (largest != i){
int swap = arrayToSort[i];
arrayToSort[i] = arrayToSort[largest];
arrayToSort[largest] = swap;
heapify(arrayToSort, n, largest);
}
}
/*
* ------------------------------------------------------
* Partition for Quick sort implementation, taken and modified from
* QuickSort.java
* ------------------------------------------------------
*/
private static int partition(int[] arrayToSort, int start, int end, int pivot){
while(start <= end){
while(arrayToSort[start] < pivot){
start++;
}
while(arrayToSort[end] > pivot){
end--;
}
if(start <= end){
int temp = arrayToSort[start];
arrayToSort[start] = arrayToSort[end];
arrayToSort[end] = temp;
start++;
end--;
}
}
return start;
}
}
【问题讨论】:
-
也许是因为你给它随机输入?
-
你能解释一下@AndyTurner 吗?
-
myArray是随机生成的。这是否意味着它必须做不同数量的工作才能使该随机数组排序? -
哦,对了,我明白了。我怎么没想到。非常感谢@AndyTurner 指出这一点。只需要对快速排序进行一些时间比较,希望它的执行速度更快!如果你愿意,你可以回答这个问题,以便我投票和接受。
-
@AndyTurner 我做了一些性能测试,Introsort 的性能比 Quicksort 差,不应该更快吗?
标签: java arrays algorithm sorting