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 Counting is the process of determining the number of elements of a finite set of objects; that is, determining the size of a set. The traditional way of counting consists of continually increasing a (mental or spoken) counter by a unit for ever ...
, 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" (analogous to a
pigeon-hole messagebox A pigeon-hole messagebox (commonly referred to as a ''pigeon-hole'' or ''pidge'', a '' cubbyhole'' (often shortened to "cubby") or simply as a ''mailbox'' in some academic or office settings) is an internal mail system commonly used for comm ...
in an office or desk), 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.


Implementation

Below is an implementation of Pigeonhole sort in
pseudocode In computer science, pseudocode is a description of the steps in an algorithm using a mix of conventions of programming languages (like assignment operator, conditional operator, loop) with informal, usually self-explanatory, notation of actio ...
. This function sorts the array in-place and modifies the supplied array. function pigeonhole(array arr) is min ← min(arr) max ← max(arr) index ← 0 range ← max - min + 1 array tmp ← new array of length range for i = 0 to length(arr) STEP 1 tmp = 0 for i = 0 to length(arr) STEP 1 tmp - min">rr - min= tmp - min">rr - min+ 1 for i = 0 to range STEP 1 while tmp > 0 do tmp = tmp - 1 arr ndex= i + min index = index + 1


Example

Suppose one were sorting these value pairs by their first element: * (5, "hello") * (3, "pie") * (8, "apple") * (5, "king") For each value between 3 and 8 we set up a pigeonhole, then move each element to its pigeonhole: * 3: (3, "pie") * 4: * 5: (5, "hello"), (5, "king") * 6: * 7: * 8: (8, "apple") The pigeonhole array is then iterated over in order, and the elements are moved back to the original list. The difference between pigeonhole sort and counting sort is that in counting sort, the auxiliary array does not contain lists of input elements, only counts: * 3: 1 * 4: 0 * 5: 2 * 6: 0 * 7: 0 * 8: 1 For arrays where ''N'' is much larger than ''n'',
bucket sort Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an Array data structure, array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recu ...
is a generalization that is more efficient in space and time.


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, of three gloves, at least two must be right-handed or at least two must be l ...
*
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:Сортировка подсчётом#Алгоритм со списком