Quickselect, based on quicksort, and counting select, based on counting sort.
Each is capable of finding the kth smallest element in an unsorted/sorted list/array.
However, I wish to write a set of guidelines to decide which of the two algorithms is most appropriate for specific situations. I need to think of situations, and than implement guidelines as to which algorithm is the better choice.
For this, I need a little help distinguishing which algorithm has specific strengths in certain areas, etc.
(1) Count sort requires the elements to be enumerable (There is a unique mapping of elements into integers) in order to work properly. Quick sort on the other hand, only require a well define compare op between every two elements (The compare function should be transitive – or intuitively – not self-contradicting).
This is one main issue you should face – the counting selection cannot always be used, while quick selection can.
Why? Well, have a look at how count sort works, or specifically – how it determines the ordering between distinct element
aand distinct elementb: It uses its integer value to count how elements of this kind are in the collection. Have a look at the pseudo-code in the wikipeida article. What I am refering to can be seen in this lines:The
key(x)value, is the enumeration of the elements – if it does not exist, what willkey(x)yield? and how will the algorithm work?This also applies for the count select algorithm for the same reasons, it also uses the
key(element)in order to count how much times this element repeats in the collection.(2) One more issue is efficiency if the range of elements is huge – where counting is inefficient, since it is linear in the range of elements.
QuickSelect, based on quicksort run time is linear on average, but may sometimes decay to quadric time complexity.
(3) Another issue is space – count selection requires additional space, linear in the range of the elements, while quick-selection does not.