Question: Consider the following implementation of Quick sort. static int partition ( int [ ] numbers, int i , int k ) { int pivot =
Consider the following implementation of Quick sort.
static int partitionint numbers, int i int k
int pivot numbersi l i h k;
while true
while numbersl pivot
l;
while pivot numbersh
h;
if l h
return h;
else
swapnumbers l h;
l;
h;
static void qsortint arr, int l int h
if l h
return;
int j partitionarr l h;
qsortarr l j;
qsortarr j h;
static void swapint arr, int i int j
int temp arri;
arri arrj;
arrj temp;
The above implementation should compile and run, eg
int arr;
qsortarr arr.length ;
aLet h l N and assume numbers is comprised of N distinct
elements in ascending order. Let CN denote the number of comparisons involving
elements of numbers performed by partitionnumbers l h
What is CN
Note that inside partition i is initialized with the value of l and k is initialized
with the value of h
b Assume numbers is comprised of N distinct elements in ascending
order. Let T N denote the number of comparisons involving elements of numbers
performed by qsortnumbers numbers.length
Give the full recurrence for T N which must explicitly use CN from the previous
part.
Justify every aspect of your recurrence.
c Solve the recurrence you gave for T N from the previous part to get a
closed form expression for T N Justify your solution through iteration or induc
tion. State your closed form expression in terms of
d Discuss whether qsort is optimal, eg is qsort optimal? Why or
why not? You must use your answers to the previous parts in your discussion.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
