快速排序比较计数

时间:2015-10-03 18:48:04

标签: java recursion quicksort

我正在尝试使用这些快速排序方法来确定发生了多少比较。我们给出了一个执行计数的全局变量,但是当我们交给它时我们无法使用全局变量。相反,我们需要递归计算比较。现在我想弄清楚如何做到这一点,我不是在寻找答案,我正在努力找到解决这个问题的正确步骤。我现在已经尝试了几个小时但没有运气。

static int qSortCompares = 0;  // GLOBAL var declaration

/**
 * The swap method swaps the contents of two elements in an int array.
 *
 * @param The array containing the two elements.
 * @param a The subscript of the first element.
 * @param b The subscript of the second element.
 */
private static void swap(int[] array, int a, int b) {
    int temp;

    temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}

 public static void quickSort(int array[]) {
    qSortCompares = 0;
    int qSCount = 0;
    doQuickSort(array, 0, array.length - 1);

}

/**
 * The doQuickSort method uses the QuickSort algorithm to sort an int array.
 *
 * @param array The array to sort.
 * @param start The starting subscript of the list to sort
 * @param end The ending subscript of the list to sort
 */
private static int doQuickSort(int array[], int start, int end) {
    int pivotPoint;
    int qSTotal = 0;
    if (start < end) {

        // Get the pivot point.
        pivotPoint = partition(array, start, end);

        // Note - only one +/=
        // Sort the first sub list.
        doQuickSort(array, start, pivotPoint - 1);

        // Sort the second sub list.
        doQuickSort(array, pivotPoint + 1, end);

    }

    return qSTotal;
}

/**
 * The partition method selects a pivot value in an array and arranges the
 * array into two sub lists. All the values less than the pivot will be
 * stored in the left sub list and all the values greater than or equal to
 * the pivot will be stored in the right sub list.
 *
 * @param array The array to partition.
 * @param start The starting subscript of the area to partition.
 * @param end The ending subscript of the area to partition.
 * @return The subscript of the pivot value.
 */
private static int partition(int array[], int start, int end) {
    int pivotValue;    // To hold the pivot value
    int endOfLeftList; // Last element in the left sub list.
    int mid;           // To hold the mid-point subscript
    int qSCount = 0;

    // see http://www.cs.cmu.edu/~fp/courses/15122-s11/lectures/08-qsort.pdf
    // for discussion of middle point - This improves the almost sorted cases
    // of using quicksort
    // Find the subscript of the middle element.
    // This will be our pivot value.
    mid = (start + end) / 2;

    // Swap the middle element with the first element.
    // This moves the pivot value to the start of
    // the list.
    swap(array, start, mid);

    // Save the pivot value for comparisons.
    pivotValue = array[start];

    // For now, the end of the left sub list is
    // the first element.
    endOfLeftList = start;

    // Scan the entire list and move any values that
    // are less than the pivot value to the left
    // sub list.
    for (int scan = start + 1; scan <= end; scan++) {
        qSortCompares++;
        qSCount++;
        if (array[scan] < pivotValue) {
            endOfLeftList++;
            // System.out.println("Pivot=" + pivotValue + "=" + endOfLeftList + ":" + scan);
            swap(array, endOfLeftList, scan);
        }
    }

    // Move the pivot value to end of the
    // left sub list.
    swap(array, start, endOfLeftList);

    // Return the subscript of the pivot value.
    return endOfLeftList;
}

/**
 * Print an array to the Console
 *
 * @param A
 */
public static void printArray(int[] A) {
    for (int i = 0; i < A.length; i++) {
        System.out.printf("%5d ", A[i]);
    }
    System.out.println();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    final int SIZE = 10;
    int[] A = new int[SIZE];

    // Create random array with elements in the range of 0 to SIZE - 1;
    System.out.printf("Lab#2 Sorting Algorithm Performance Analysis\n\n");

    for (int i = 0; i < SIZE; i++) {
        A[i] = (int) (Math.random() * SIZE);
    }

    System.out.printf("Unsorted Data = %s\n", Arrays.toString(A));

    int[] B;

    // Measure comparisons and time each of the 4 sorts
    B = Arrays.copyOf(A, A.length);  // Need to do this before each sort
    long startTime = System.nanoTime();
    quickSort(B);
    long timeRequired = (System.nanoTime() - startTime) / 1000;

    System.out.printf("Sorted Data = %s\n", Arrays.toString(B));
    System.out.printf("Number of compares for quicksort     = %8d time = %8d us Ratio = %6.1f compares/us\n", qSortCompares, timeRequired, qSortCompares / (double) timeRequired);

    // Add code for the other sorts here ...
}

说明给出了一些提示,但我仍然迷失了:

  

quicksort方法目前使用全局变量计算比较次数。这不是一个好的编程技术。修改quicksort方法以通过传递参数来计算比较。由于在分区方法中进行了比较,因此这有点棘手。您应该能够看到在调用分区方法之前可以确定比较次数。您需要从Quicksort方法返回此值,并修改quickSort标头以将此值传递到每个递归调用中。您需要递归添加计数。

     

作为递归计数的替代方法,您可以保留代码并完成实验,而无需修改。

我一直在看这个赋值的方式我在名为qSCount的分区方法中创建了一个变量,当它被调用时将计算进行了多少次比较。但是我无法使用该变量,因为我没有返回它。而且我不确定如何在该状态下使用递归。我的想法是每次qSCount有一个值后,我可以以某种方式将它存储在qSTotal下的doQuickSort方法中。但后来暗示我需要在quicksort中创建一个参数,所以我很困惑。

1 个答案:

答案 0 :(得分:0)

为了使用递归方法计算某些东西(没有全局变量),我们需要返回它。你有:

private static int doQuickSort(int array[], int start, int end)

这是正确的想法。但由于比较实际上发生在

之内

private static int partition(int array[], int start, int end)

你需要让分区返回多少次比较。

这给我们留下了两个选择:

  1. 我们可以创建或使用现有的Pair类,让这个方法返回一对整数,而不只是一个整数(透视)。
  2. 我们可以创建一个计数器类并传递一个计数器对象并在那里完成计数。这消除了返回另一个值的需要,因为该参数可用于增加计数。
相关问题