In this assignment, you are given two functions quicksort , select, and partitio
ID: 3618039 • Letter: I
Question
In this assignment, you are given two functionsquicksort, select, andpartition. You are required to do thefollowing:
One input to your program is the following:
3 5 7 26 18 20 11 25 17 27 9 13 24 8 28 14 5 19 23 6 29 4 30 1822 10 16 21
Quicksort:
void quicksort(int a[], int l, int r)
{
if (r <= l) return;
/* use function select to choose the medianelement as pivot and partition around this element */
quicksort(a, l, i-1);
quicksort(a, i+1, r);
}
Selecting kth smallest element:
void select(int a[], int l, int r, int k)
{
if (r <= l) return;
int i = partition(a, l, r);
if (i > l+k-1) select(a, l, i-1, k);
if (i < l+k-1) select(a, i+1, r, k-i);
}
Partition:
int partition(int a[], int l, int r)
{ int i = l-1, j = r; int v = a[r];
for (;;)
{
while (a[++i] < v);
while (v < a[--j])if (j == l) break;
if (i >= j)break;
exch(a[i], a[j]);
}
exch(a[i], a[r]);
return i;
}
Explanation / Answer
solved