ArticleslgStudy

computer science

Pigeonhole sort

Pigeonhole sort is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Pigeonhole sort rather than just read about it. In short: Pigeonhole sorting is a sorting algorithm 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.

Key takeaways

  • Pigeonhole sort belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Pigeonhole sort to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Pigeonhole sort from memory before moving on to harder problems.

Reference excerpt

Pigeonhole sorting is a sorting algorithm 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, but differs in that it "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'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 in an office or desk), one pigeonhole for each key in the range 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. 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 range STEP 1 tmp[i] = 0

for i = 0 to length(arr) STEP 1 tmp[arr[i] - min] = tmp[arr[i] - min] + 1

for i = 0 to range STEP 1 while tmp[i] > 0 do tmp[i] = tmp[i] - 1 arr[index] = 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 is a generalization that is more efficient in space and time.

See also Pigeonhole principle Radix sort Bucket queue, a related priority queue data structure

References

Worked examples

Example 1 — a first encounter with Pigeonhole sort

Start with the simplest possible case. Write down what Pigeonhole sort claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Pigeonhole sort before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Pigeonhole sort ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Pigeonhole sort

In research
Pigeonhole sort appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Pigeonhole sort in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Pigeonhole sort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Sorting algorithms, Stable sorts, so understanding it makes those chapters shorter.
In everyday life
Look for Pigeonhole sort outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Pigeonhole sort” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Pigeonhole sort in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Pigeonhole sort means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Pigeonhole sort out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Pigeonhole sort in simple terms?

Pigeonhole sorting is a sorting algorithm 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.

Why does Pigeonhole sort matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Pigeonhole sort?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Pigeonhole sort.

Tags

  • Sorting algorithms
  • Stable sorts

Keep exploring