
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































ā
ā Can submit by Monday at 2:15PM using one late period. ā
ā (^) Play around with divide-and-conquer algorithms and recurrence relations! ā Covers material up through and including today's lecture.
ā
ā A problem halfway between searching and sorting. ā
ā (^) A nonobvious algorithm with a nontrivial runtime. ā
ā (^) Solving recurrences the Master Theorem can't handle.
ā Given a collection of data, the k th order statistic is the k th smallest value in the data set. ā For the purposes of this course, we'll use zero-indexing, so the smallest element would be given by the 0 th order statistic. ā To give a robust definition: the k th order statistic is the element that would appear at position k if the data were sorted.
ā
ā
ā Sort the array. ā Return the element at the k th position. ā
ā
ā Given an input array, a partition algorithm chooses some element p (called the pivot ), then rearranges the array so that ā All elements less than or equal to p are before p. ā (^) All elements greater p are after p. ā p is in the position it would occupy if the array were sorted. ā The algorithm then returns the index of p. ā We'll talk about how to choose which element should be the pivot later; right now, assume the algorithm chooses one arbitrarily.
ā There is a close connection between partitioning and the selection problem. ā Let k be the desired index and p be the pivot index after a partition step. Then: ā If p = k , return A[ k ]. ā If p > k , recursively select element k from the elements before the pivot. ā If p < k , recursively select element ( k ā p ā 1) from the elements after the pivot.
ā
ā Check the Problem Set Advice handout for an outline of an algorithm to do this. ā
ā
ā The runtime of our algorithm depends on our choice of pivot. ā In the best-case, if we pick a pivot that ends up at position k , the runtime is Ī( n ). ā In the worst case, we pick always pick pivot that is the minimum or maximum value in the array. The runtime is given by this recurrence:
ā
2
ā
ā
ā
ā
ā (^) (Those sizes don't have to be the same, though.)
ā
ā Recursively calls itself on the first 2/3 of the array. ā Runs a partition step. ā Then, either immediately terminates, or recurses in a piece of size n / 3 or a piece of size 2 n / 3. ā
ā
ā
ā
ā a = 2 , b = 3 / 2 , and d = 1.
3 / 2
log 3 / 2 2