I’ve been given the basic code for an algorithm, which selects the kth smallest element in an unsorted array (or sorted, I’m not sure). Usually, we’d use quickselect for this, but we’ve been given another choice which has been labelled ‘countingselect’ as the function name.
“Counting select uses a similar approach to counting sort. Items in the list are used as indexes into an array of counts. Then, starting at the low value end of the array, item counts are accumulated until the total exceeds the desired value.”
// return the kth smallest item
int countingSelect(int items[], int first, int last, int k) {
int counts[cap];
for (int c = 0; c < cap; c++) {
counts[c] = 0;
}
for (int i = first; i < last; i++) {
counts[items[i]] += 1;
}
int c = 0;
while (k >= 0) {
k -= counts[c++];
}
return c-1;
}
I’m having enormous trouble breaking this down into pseudo-code, so that I can understand exactly how the function works. With the code we’ve been given, my first confusion is what the value ‘cap’ is, and what it’s function is. What value is cap usually? I haven’t been given this information.
Breaking down the algorithm into pseudo code is a good way to understand it, and I request some aid in breaking it down and stepping through the code.
Thankyou in advance.
If you have understood count sort, this should be quite simple. If not, then let me briefly say how count sort works.
Say you have 10 numbers, in the range [0, 15]. Since you know the bound of data, you can go over your input, and mark how many times you see each number. Then you iterate over the counts and retrieve the numbers in order:
Let’s see an example:
The first loop creates the counts:
And the second loop gives you:
that is:
Your algorithm is basically the same, except instead of outputting a sorted list, it find the kth smallest item.
In the example above, you can see that:
If you look closely, you will see this pattern:
However, the numbers
are in fact the running sum of
countsitself!So, what the bottom of your algorithm does is a running sum, except it checks when the sum gets bigger than
k. When it does, the previous number had been your answer. Note that the indices tocountsare the numbers themselves.Your algorithm has tried to avoid the
sumvariable and instead subtracts the running sum fromkitself which has the same effect.