简介排序(快速排序+堆排序)不持久?

时间:2017-03-12 16:28:03

标签: java arrays algorithm sorting

我尝试实施Introsort,它以quicksort算法开头,但一旦递归深度达到heapsort,就会切换到2 * log(array length)

我尝试在小型数组(数组大小= 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;
}

}

1 个答案:

答案 0 :(得分:1)

myArray是随机生成的。

这意味着算法必须做不同的工作量才能将随机数组按顺序排列。

多次传递相同的数组将产生相同的计数。

请注意,通过将这些作为静态变量,您必须重置调用之间的计数;此外,用于并行调用也不安全。您可能想要考虑不涉及这种可变全局状态的替代方案。