简体   繁体   中英

QuickSort Algorithm Improvement

I am doing an algorithms class project in which we must modify an implementation of QuickSort with suggested improvements. One of these suggestions is as follows: Do not single out the pivot in the array and avoid the last swap of the partition method.

I'm having trouble understanding exactly what he means by this. Without a pivot, how is it even still QuickSort anymore? Any insight into what this could imply would be appreciated. This is the Java code to be modified.

public void quickSort() {
    recQuickSort(0, nElems - 1);
}

public void recQuickSort(int left, int right) {
    if (left >= right)
        return;
    long pivot = a[right];
    int mid = partition(left, right, pivot);
    recQuickSort(left, mid - 1);
    recQuickSort(mid + 1, right);
} // end recQuickSort()

public void swap(int dex1, int dex2) { // swap two elements
    long temp = a[dex1]; // A into temp
    a[dex1] = a[dex2]; // B into A
    a[dex2] = temp; // temp into B
} // end swap()

public int partition(int left, int right, long pivot) {
    // assuming pivot == a[right]
    int leftPtr = left - 1; // left of the first element
    int rightPtr = right; // position of pivot
    while (true) {
        while (a[++leftPtr] < pivot)
            ; // find bigger
        while (leftPtr < rightPtr && a[--rightPtr] >= pivot)
            ; // find smaller
        if (leftPtr >= rightPtr) // if pointers cross,
            break; // partition done
        else
            // not crossed, so
            swap(leftPtr, rightPtr); // swap elements
    } // end while(true)
    swap(leftPtr, right); // restore pivot
    return leftPtr; // return pivot location
} // end partition()

I'm not going to try to implement it for you, but my interpretation of this suggested "improvement" is that he wants you to still choose a pivot value and partition the array into sections according to which side of that value they're on, but not treat the array entry containing that value specially.

It shouldn't be hard to do, but it won't improve performance of the algorithm at all, and I doubt that it's much of an improvement in any other sense.

It looks like he doesn't want you to pivot on an actual element in the array. The point of the last swap in the partition() method is that he moves the pivot into the correct spot in the array (note that he never moves a pivot element after a partition() call).

Edit If you are confused about "not using a pivot", you are still using a pivot... it's just not a pivot in the array. Imagine doing the quicksort by hand, you can pick any arbitrary value to pivot with.

The issue is that that swap doesn't really negatively impact performance at all. On the other hand, this should be a quick change...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM