

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
Sieve Technique, Applying the Sieve to Selection, Partitioned about the pivot x, Selection Algorithm, Sieve example, Pivot element, Constant fraction, Recursive solutions are the key points in this study notes file.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


The reason for introducing this algorithm is that it illustrates a very important special case of divide-and-conquer, which I call the sieve technique. We think of divide-and-conquer as breaking the problem into a small number of smaller subproblems, which are then solved recursively. The sieve technique is a special case, where the number of subproblems is just 1.
The sieve technique works in phases as follows. It applies to problems where we are interested in finding a single item from a larger set of n items. We do not know which item is of interest, however after doing some amount of analysis of the data, taking say Θ(nk) time, for some constant k, we find that we do not know what the desired the item is, but we can identify a large enough number of elements that cannot be the desired value, and can be eliminated from further consideration. In particular “large enough” means that the number of items is at least some fixed constant fraction of n (e.g. n/2, n/3). Then we solve the problem recursively on whatever items remain. Each of the resulting recursive solutions then do the same thing, eliminating a constant fraction of the remaining set.
To see more concretely how the sieve technique works, let us apply it to the selection problem. We will begin with the given array A[1..n]. We will pick an item from the array, called the pivot element which we will denote by x. We will talk about how an item is chosen as the pivot later; for now just think of it as a random element of A. We then partition A into three parts.
Within each sub array, the items may appear in any order. The following figure shows a partitioned array:
Figure 3.4: A[p..r] Partitioned about the pivot x
It is easy to see that the rank of the The rank of the pivot x is q - p + 1 in A[p..r]. Let rank x = q - p + 1. If k = rank x then the pivot is kth smallest. If k < rank x then search A[p..q - 1] recursively. If k > rank x then search A[q + 1..r] recursively. Find element of rank (k - q) because we eliminated q smaller elements in A.
SELECT( array A, int p, int r, int k) 1 if (p = r) 2 then return A[p] 3 else x ← CHOOSE PIVOT(A, p, r) 4 q ← PARTITION(A, p, r, x) 5 rank x ← q - p + 1 6 if k = rank x 7 then return x 8 if k < rank x 9 then return SELECT(A, p, q - 1, k) 10 else return SELECT(A, q + 1, r, k - q)
Example: select the 6 th smallest element of the set {5, 9, 2, 6, 4, 1, 3, 7}
Figure 3.5: Sieve example: select 6 th smallest element