Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A detailed difference between Iterative Quicksort and Selection Quicksort. Inclu

ID: 3666122 • Letter: A

Question

A detailed difference between Iterative Quicksort and Selection Quicksort. Including the following points:
A discussion of the critical operation that you involves both recursive and iterative quicksort and their differences.
A Big- analysis of the two versions of the algorithm.
Sample results of each run of algorithms, which should include:
-graphs of results
-a comparison of the performance of the two versions of the algorithm
-a comparison of the critical operation results and the actual execution time measurements
-a discussion of the significance of the standard deviation results and how it reflects the data sensitivity of the algorithm
-how sample results of each algorithm compare to your Big- analysis

Explanation / Answer

Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
1) Always pick first element as pivot.
2) Always pick last element as pivot (implemented below)
3) Pick a random element as pivot.
4) Pick median as pivot.

The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

Partition Algorithm
There can be many ways to do partition, following code adopts the method given in CLRS book. The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.

int partition (int arr[], int l, int h)

{

    int x = arr[h];    // pivot

    int i = (l - 1); // Index of smaller element

    for (int j = l; j <= h- 1; j++)

    {

        // If current element is smaller than or equal to pivot

        if (arr[j] <= x)

        {

            i++;    // increment index of smaller element

            swap(&arr[i], &arr[j]); // Swap current element with index

        }

    }

    swap(&arr[i + 1], &arr[h]);

    return (i + 1);

}

/* arr[] --> Array to be sorted, l --> Starting index, h --> Ending index */

void quickSort(int arr[], int l, int h)

{

    if (l < h)

    {

        int p = partition(arr, l, h); /* Partitioning index */

        quickSort(arr, l, p - 1);

        quickSort(arr, p + 1, h);

    }

}