Question

In: Computer Science

If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and...

If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and runs PARTITION until it finds a good pivot, and suppose we keep track of the pivots used so far so we never use one twice for the same array, what is the worst case cost of the algorithm? Give a recurrence for this and its asymptotic solution (you can use the master method.)

Solutions

Expert Solution

The Worst-Case complexity is still O( n^2 ). But Using random pivoting we improve the expected or average time complexity to O(nlogn).

Recurrence for radomized Quicksort is - T(n) = T(k − 1) + T(n − k) + Θ(n) , Eq.(12)

where the value k − 1 is the number of elements on the left side of the recursion tree, i.e., k tells us how good (balanced) the split is. If k = 1 then we have the worst case and if k = n/2 we have the best-case. More generally, if k = O(1) the running time is Θ(n^2 ) and if k = O(n) the running time is O(n log n). In Randomized Quicksort, all values of k are equally likely and thus the average running time is given by averaging Eq. (12) over all k:

     

where we have made the asymptotic substitution Θ(n) = c1n, for some constant c1. We can simplify this by substituting i = k − 1 in the first summation and i = n − k in the second, and then multiplying through by n:

Eq.(13)

Now we’ll use a technique you can use to solve some recurrence relations: subtract a recurrence for a problem of size n − 1 from the recurrence for a problem of size n. Using Eq. (13), this yields:

   Eq.(14) & Eq.(15)

Where we divided both sides of Eq. (14) by n*(n + 1) to produce Eq. (15).

Now, one last change of variables: let T(n)/(n + 1) = D(n), and thus T(n − 1)/n = D(n − 1). This yields:

D(n) = D(n − 1) + O(1/n)

= O(log n) ,

where the last line follows from a bound on the harmonic series. Thus, reversing the substitution for D(n), we see that T(n) = O(n log n).


Related Solutions

in C++, Modify the quicksort algorithm such that it uses the last item as the pivot...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order. NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array. After it has been sorted in descending order, go through all the items in the array and make...
Write an implementation of quicksort where the pivot is selected randomly.
Write an implementation of quicksort where the pivot is selected randomly.
What is the reason to choose a random pivot element in Randomized Quicksort? How can using...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using randomization in this way possibly help?
Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.
Programming Language : JavaModify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.After it has been sorted in descending order, go through all the items in the array and make sure...
We consider a randomized experiment, the Tennessee STAR experiment, where students and teachers are randomly assigned...
We consider a randomized experiment, the Tennessee STAR experiment, where students and teachers are randomly assigned to either a small class (15 students) and a regular class (24 students). We want to estimate the effect of smaller class in primary school and use the following linear model: Score = β0 + β1ClassSize + Controls + u, where Score is student’s academic score, Class Size is dummy for small class, and controls includes free lunch status, race, gender, teacher characteristics and...
Hi, so for my homework assignment, we are meant to change this function so that instead...
Hi, so for my homework assignment, we are meant to change this function so that instead of making a list from the array going up in an ascending order, making it be listed in descending order, meaning going from largest number to smallest number, the code I have now works for the ascending order, but how would I change this for it to go descending? Any help would be amazing, and please with a tutorial, thanks so much. def heapify(arr,...
We considered the change in the number of days exceeding 90'F from 1948 and 2018 at 197 randomly sampled locations from the NOAA database in Exercise 7.19.
7.21 Global warming, Part II. We considered the change in the number of days exceeding 90'F from 1948 and 2018 at 197 randomly sampled locations from the NOAA database in Exercise 7.19. The mean and standard deviation of the reported differences are 2.9 days and 17.2 days. (a) Calculate a 90% confidence interval for the average difference between number of days exceeding 90°F between 1948 and 2018. We've already checked the conditions for you. (b) Interpret the interval in context. (c) Does the confidence...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT