Data Structures and Algorithms -- CSci 230
Chapter 7 -- Sorting

Overview


Sorting algorithms already covered


Heap Sort


Exercise


Hand simulate several iterations of Insertion Sort , Merge Sort and Heap Sort on the following array


1c0 1c1 1c2 1c3 1c4 1c5 1c6 1c7 1c8 1c9
9.5 3.5 9.1 14.4 8.7 17.8 6.4 9.3 7.5 1.9

Quick Sort -- basic version


Exercises


1.
Hand simulate B_Q_Sort on the array
1c0 1c1 1c2 1c3 1c4 1c5 1c6 1c7 1c8 1c9
9.5 3.5 9.1 14.4 8.7 17.8 6.4 9.3 7.5 1.9

up until the first recursive calls are made. What are these recursive calls?

2.
In the function B_Q_Sort, suppose we replaced the inner two while loops with the statements
    while( A[ i ] < Pivot ) i++;
    while( A[ j ] > Pivot ) j--;
Would the function still work properly? As a hint, suppose all values from A[Left] to A[Right] are equal.

Analysis of Quick Sort


Exercise


Analyze the worst-case for T(n). First try to do this based on intuition and then more carefully. What condition of array A causes this?

Median-of-Three Quick Sort


Exercises


1.
Hand simulate Q_Sort with Left=0 and Right=14 on the following array. What are the recursive calls? Stop your simulation after identifying the recursive calls.
1c0 1c1 1c2 1c3 1c4 1c5 1c6 1c7 1c8 1c9 1c10 1c11 1c12 1c13 1c14
90.1 16.2 50.5 40.7 15.0 89.7 36.8 3.0 14.2 30.9 28.6 50.1 32.3 21.1 27.5

2.
Why does Q_Sort use i to swap at the end (with Right-1) whereas B_Q_Sort uses j to swap at the end (with Left)?

3.
How would you modify Q_Sort to make it non-recursive?

Quick Select


Review Problems


1.
The second version quick sort discussed in class uses the median-of-three technique to find the pivot and does not continue sorting when the length of the sublist (the difference between Right and Left is less than Cutoff). After this algorithm completes, insertion sort is run over the resulting array to bring the values into their final sorted position. What is the worst-case time required by insertion sort in this case? Justify your answer.
2.
Here is a slightly different version of the Quick Select algorithm, which is much closer to the original version of Quick Sort. At the end of the function (including all recursive calls), the desired value will be in the kth location of the array.
Q_Select_One ( int A[ ], const int k, const int Left, const int Right )
{
    if( Left < Right ) {
        int Pivot = A [Left];
        unsigned int i = Left, j = Right + 1;

        for( ; ; ) {
            while( A[ ++i ] < Pivot && i<Right );
            while( A[ --j ] > Pivot );
            if( i < j )
                Swap( A[ i ], A[ j ] );
            else
                break;
        }
        Swap( A[ j ], A[ Left ] ); // Move pivot to sorted position.

        if( k < j )       Q_Select_One( A, k, Left, j-1 );
        else if( k > j )  Q_Select_One( A, k, j+1, Right );
    }
}

(a)
Assume the original call is made to this function with
        Q_Select_One( A, 3, 0, 8)
with the following contents of A
1c0 1c1 1c2 1c3 1c4 1c5 1c6 1c7 1c8
14 24 8 16 32 71 26 25 12

Show the contents of A near the end of the code, just before entering the conditional to check if a recursive call should be made. What recursive call, if any, is made?

(b)
What is the worst-case number of comparisons in Q_Select_One as a function of both n and k, where n is the value of Right-Left+1 in the first call? When does this occur?

3.
Solve the Quick Sort recursive function

T(n) = T(k) + T(n-k-1) + n-1

to yield a non-recursive form when k=1. Assume that T(0) = T(1)=0 and T(2)=2. You may assume that n is even.



 

Charles Stewart
10/14/1998