Pigeonhole Sort
   HOME

TheInfoList



OR:

__NOTOC__ Pigeonhole sorting is a
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. ...
that is suitable for sorting lists of elements where the number ''n'' of elements and the length ''N'' of the range of possible key values are approximately the same. It requires O(''n'' + ''N'') time. It is similar to
counting sort In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small positive integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that possess di ...
, but differs in that it "moves items twice: once to the bucket array and again to the final destination hereascounting sort builds an auxiliary array then uses the array to compute each item's final destination and move the item there." The pigeonhole algorithm works as follows: # Given an array of values to be sorted, set up an auxiliary array of initially empty "pigeonholes", one pigeonhole for each key in the
range Range may refer to: Geography * Range (geographic), a chain of hills or mountains; a somewhat linear, complex mountainous or hilly area (cordillera, sierra) ** Mountain range, a group of mountains bordered by lowlands * Range, a term used to i ...
of the keys in the original array. # Going over the original array, put each value into the pigeonhole corresponding to its key, such that each pigeonhole eventually contains a list of all values with that key. # Iterate over the pigeonhole array in increasing order of keys, and for each pigeonhole, put its elements into the original array in increasing order.


See also

*
Pigeonhole principle In mathematics, the pigeonhole principle states that if items are put into containers, with , then at least one container must contain more than one item. For example, if one has three gloves (and none is ambidextrous/reversible), then there mu ...
*
Radix sort In computer science, radix sort is a non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according to their radix. For elements with more than one significant digit, this bucketing process i ...
*
Bucket queue A bucket queue is a data structure that implements the priority queue abstract data type: it maintains a dynamic collection of elements with numerical priorities and allows quick access to the element with minimum (or maximum) priority. In the bu ...
, a related priority queue data structure


References

{{sorting Sorting algorithms Stable sorts ru:Сортировка подсчётом#Алгоритм со списком