<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.tachyony.co.uk/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=14.241.246.183</id>
	<title>Wikipedia - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.tachyony.co.uk/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=14.241.246.183"/>
	<link rel="alternate" type="text/html" href="https://wiki.tachyony.co.uk/wiki/Special:Contributions/14.241.246.183"/>
	<updated>2026-07-08T09:10:01Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.tachyony.co.uk/w/index.php?title=Pigeonhole_sort&amp;diff=31513</id>
		<title>Pigeonhole sort</title>
		<link rel="alternate" type="text/html" href="https://wiki.tachyony.co.uk/w/index.php?title=Pigeonhole_sort&amp;diff=31513"/>
		<updated>2025-10-14T07:31:05Z</updated>

		<summary type="html">&lt;p&gt;14.241.246.183: correcting by change the first &amp;quot;length(arr)&amp;quot; to &amp;quot;range&amp;quot; of the pseudocode&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Short description|Sorting algorithm}}&lt;br /&gt;
{{more citations needed|date=July 2017}}&lt;br /&gt;
__NOTOC__&lt;br /&gt;
&lt;br /&gt;
{{Infobox Algorithm&lt;br /&gt;
|class=[[Sorting algorithm]]&lt;br /&gt;
|image=&lt;br /&gt;
|data=[[Array data structure|Array]]&lt;br /&gt;
|time=&amp;lt;math&amp;gt;O(N+n)&amp;lt;/math&amp;gt;, where &#039;&#039;N&#039;&#039; is the range of key values and &#039;&#039;n&#039;&#039; is the input size &lt;br /&gt;
|space=&amp;lt;math&amp;gt;O(N+n)&amp;lt;/math&amp;gt;&lt;br /&gt;
|optimal=If and only if &amp;lt;math&amp;gt;N=O(n)&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&#039;&#039;&#039;Pigeonhole sorting&#039;&#039;&#039; is a [[sorting algorithm]] that is suitable for sorting lists of elements where the number &#039;&#039;n&#039;&#039; of elements and the length &#039;&#039;N&#039;&#039; of the range of possible key values are approximately the same.&amp;lt;ref&amp;gt;{{cite web| url = https://xlinux.nist.gov/dads/HTML/pigeonholeSort.html| title = NIST&#039;s Dictionary of Algorithms and Data Structures: pigeonhole sort}}&amp;lt;/ref&amp;gt; It requires [[big O notation|O]](&#039;&#039;n&#039;&#039; + &#039;&#039;N&#039;&#039;) time.  It is similar to [[counting sort]], but differs in that it &amp;quot;moves items twice: once to the bucket array and again to the final destination [whereas] counting sort builds an auxiliary array then uses the array to compute each item&#039;s final destination and move the item there.&amp;quot;&amp;lt;ref&amp;gt;{{cite web|last1=Black|first1=Paul E.|title=Dictionary of Algorithms and Data Structures|url=https://xlinux.nist.gov/dads/HTML/pigeonholeSort.html|website=NIST|access-date=6 November 2015}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The pigeonhole algorithm works as follows:&lt;br /&gt;
#  Given an array of values to be sorted, set up an auxiliary array of initially empty &amp;quot;pigeonholes&amp;quot; (analogous to a [[pigeon-hole messagebox]] in an office or desk), one pigeonhole for each key in the [[Range (computer science)|range]] of the keys in the original array.&lt;br /&gt;
#  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.&lt;br /&gt;
#  Iterate over the pigeonhole array in increasing order of keys, and for each pigeonhole, put its elements into the original array in increasing order.&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
Below is an implementation of Pigeonhole sort in [[pseudocode]]. This function sorts the array in-place and modifies the supplied array. &lt;br /&gt;
 &#039;&#039;&#039;function&#039;&#039;&#039; pigeonhole(&#039;&#039;&#039;array&#039;&#039;&#039; arr) &#039;&#039;&#039;is&#039;&#039;&#039; &lt;br /&gt;
     min ← &#039;&#039;&#039;min&#039;&#039;&#039;(arr)&lt;br /&gt;
     max ← &#039;&#039;&#039;max&#039;&#039;&#039;(arr) &lt;br /&gt;
     index ← 0&lt;br /&gt;
     range ← max - min + 1&lt;br /&gt;
     &#039;&#039;&#039;array&#039;&#039;&#039; tmp ← new array of length range&lt;br /&gt;
 &lt;br /&gt;
     &#039;&#039;&#039;for&#039;&#039;&#039; i = 0 &#039;&#039;&#039;to&#039;&#039;&#039; range &#039;&#039;&#039;STEP&#039;&#039;&#039; 1&lt;br /&gt;
         tmp[i] = 0&lt;br /&gt;
 &lt;br /&gt;
     &#039;&#039;&#039;for&#039;&#039;&#039; i = 0 &#039;&#039;&#039;to&#039;&#039;&#039; &#039;&#039;&#039;length(&#039;&#039;&#039;arr&#039;&#039;&#039;)&#039;&#039;&#039; &#039;&#039;&#039;STEP&#039;&#039;&#039; 1&lt;br /&gt;
         tmp[arr[i] - min] = tmp[arr[i] - min] + 1 &lt;br /&gt;
 &lt;br /&gt;
     &#039;&#039;&#039;for&#039;&#039;&#039; i = 0 &#039;&#039;&#039;to&#039;&#039;&#039; range &#039;&#039;&#039;STEP&#039;&#039;&#039; 1&lt;br /&gt;
         &#039;&#039;&#039;while&#039;&#039;&#039; tmp[i] &amp;gt; 0 &#039;&#039;&#039;do&#039;&#039;&#039;&lt;br /&gt;
             tmp[i] = tmp[i] - 1&lt;br /&gt;
             arr[index] = i + min&lt;br /&gt;
             index = index + 1&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Suppose one were sorting these value pairs by their first element:&lt;br /&gt;
&lt;br /&gt;
* (5, &amp;quot;hello&amp;quot;)&lt;br /&gt;
* (3, &amp;quot;pie&amp;quot;)&lt;br /&gt;
* (8, &amp;quot;apple&amp;quot;)&lt;br /&gt;
* (5, &amp;quot;king&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
For each value between 3 and 8 we set up a pigeonhole, then move each element to its pigeonhole:&lt;br /&gt;
&lt;br /&gt;
* 3: (3, &amp;quot;pie&amp;quot;)&lt;br /&gt;
* 4:&lt;br /&gt;
* 5: (5, &amp;quot;hello&amp;quot;), (5, &amp;quot;king&amp;quot;)&lt;br /&gt;
* 6:&lt;br /&gt;
* 7:&lt;br /&gt;
* 8: (8, &amp;quot;apple&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The pigeonhole array is then iterated over in order, and the elements are moved back to the original list.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
* 3: 1&lt;br /&gt;
* 4: 0&lt;br /&gt;
* 5: 2&lt;br /&gt;
* 6: 0&lt;br /&gt;
* 7: 0&lt;br /&gt;
* 8: 1&lt;br /&gt;
&lt;br /&gt;
For arrays where &#039;&#039;N&#039;&#039; is much larger than &#039;&#039;n&#039;&#039;, [[bucket sort]] is a generalization that is more efficient in space and time.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Pigeonhole principle]]&lt;br /&gt;
* [[Radix sort]]&lt;br /&gt;
* [[Bucket queue]], a related priority queue data structure&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Wikibooks|Algorithm implementation|Sorting/Pigeonhole sort|Pigeonhole sort}}&lt;br /&gt;
{{sorting}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Sorting algorithms]]&lt;br /&gt;
[[Category:Stable sorts]]&lt;br /&gt;
&lt;br /&gt;
[[ru:Сортировка подсчётом#Алгоритм со списком]]&lt;/div&gt;</summary>
		<author><name>14.241.246.183</name></author>
	</entry>
</feed>