Reputation: 3537
From the "Cormen Leiserson Rivest Stein, 3th Edition, Problem 9-1, point C, pag. 224", I have the following assignment:
Given a set (array) A of n numbers, use an order-statistic algorithm to find the i-th largest number, partition around that number, and sort the i largest numbers.
I used the Randomized-Select algorithm (from the same book, pag. 216 - it uses the Randomized-Partition algorithm) that finds the i-th smallest number, while I want to find the i-th largest number (let's call it "k-th" to avoid confusion). Basically, I can obtain the k-th largest number as:
n - ith + 1
Then I call RandomizedSelect() to find the k-th largest number and all works great!
Here there is an example (in C) of how I find the 4-th largest number:
int A[10] = {3, 20, 15, 4, 1, 9, 18, 64, 22, 5}; // the given A set
int ith = 4; // I want to find the 4-th largest number of A
int kth = 10 - ith + 1; // I do this "conversion" for the reasons I explained above
int i = RandomizedSelect(A, 0, 9, kth); // it returns the index of A pointing to the 4-th largest number
printf("A vector: ");
for (j = 0; j < 10; j++) printf ("%u ", A[j]); // prints the A vector partially "ordered"
printf("\n4-th largest number: %u", A[i]); // prints the 4-th largest number
And here there is an example of the output:
A vector: 3 1 4 5 9 15 18 64 22 20
4-th largest number: 18
Now I want not only the 4-th largest number, but also the others 4 largest numberS in an ordered way (from the example: 18 20 22 64). So I just run for example MergeSort() on the A vector, starting from the i-th index found before until the end. The output will be: 18 20 22 64.
The problem is that the assignment says that I have to partition around the i-th (4-th) largest number and order the other i (4) largest numbers, and then run MergeSort() as explained before. But I can't understand why should I do that... In my example, partitioning around 18 means nothing, because I tried and this has been the output of the A vector after I did that partition (calling SelectedPartition()):
3 1 4 5 9 15 18 64 22 20
18
...it's the same output!
So, what did I misunderstand of the assignment? Or, am I doing it in a better way?
Upvotes: 2
Views: 1038
Reputation: 372982
There are many different algorithms for computing order statistics. Many, like quickselect or the median-of-medians algorithm, automatically partition the array around the kth element as part of their implementation. However, there is no guarantee that the selection algorithm has to do this. For example, you could implement selection by putting all the elements into an order statistic tree data structure, then querying the kth element. As a result, it is most correct to put in the explicit partition step to ensure the partition happens.
In your case, since you're using an algorithm that already does a partition, you can safely ignore this step.
Hope this helps!
Upvotes: 2