

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Kowalczyk; Class: Algorithms Design And Analysis; Subject: Computer Science; University: Northern Michigan University; Term: Winter 2008;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Algorithm Design and Analysis 2/25/
Instructor: Mike Kowalczyk
First, let’s define the problem carefully.
Selection problem: Input: Array of n elements, and index 0 ≤ k ≤ n − 1. Output: Returns A′[k], where A′^ is the sorted version of A.
Note that our problem definition doesn’t necessarily say that we actually sort the array; it’s just a way of specifying the output.
Our idea starts with grouping the n elements in sets of 5 each. Then, sort each group of 5, take the median of each of group of 5, and recursively find the median of those medians-of-5. Now, we know that the median-of-medians is going to be greater than approximately 3n/10 elements. Similarly, it’s less than 3n/10 elements, so the median-of-medians is somewhere from the 3/10 mark to the 7/10 mark. Because of this, we can use the median-of-medians as a pivot, and split up the remaining work, just like Quicksort. The fact that the pivot is roughly in the middle means that it should be fast, but we’ll prove this to make sure.
selection(array A, index k) { Group A into sets of 5 elements each. Sort each set of 5. Let M be the set of medians of those sets of 5. if (M is size 1) return M[0]; p = selection(M, M.length / 2); //finds the median of M //Use the pivot, p, to split A into arrays S and B. Let S = the elements of A that are smaller than p Let B = the elements of A that are bigger than p if (|S| == k) { //|S| denotes the number of elements in S return p; } else if (k < |S|) { return selection(S, k); } else { //k > |S|
return selection(B, k - |S|); } }
At level i, we have that the total size of all nodes’ arrays added up is ( 109 )in. Each node takes only cn time for the splitting up of the problem and so forth (everything but the recursive calls), where c is a constant. We have depth is i where ( 109 )in = 1. This is equivalent to n = ( 109 )i. So i = log 10 / 9 n. So the total runtime is
log ∑ 10 / 9 n
i=
)icn = cn
log ∑ 10 / 9 n
i=
)i
≤ cn
i=
)i
= 10 cn = Θ(n).