Pigeonhole Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Pigeonhole Sort
A simple, non-comparison sorting algorithm that is ideal when the range of values is approximately equal to the number of elements.
How it works
Pigeonhole Sort is closely related to Counting Sort, but instead of maintaining a count array and accumulating prefix sums, it maps elements directly into a set of "pigeonholes" (lists) corresponding to each possible value in the range, and then copies them back.
- Find the minimum and maximum values in the input array.
- Calculate the range:
range = max - min + 1. - Set up an array of empty "pigeonholes" (lists) of size equal to the range.
- Scan the input array and append each element
xto the list at indexx - min. - Iterate through the pigeonholes in order and write the elements back to the original array.
Complexity
Interactive visualization
Pseudocode
procedure pigeonholeSort(A): n ← length(A) min, max ← findMinMax(A) range ← max - min + 1 holes ← array of range empty lists for i from 0 to n - 1: holes[A[i] - min].append(A[i]) idx ← 0 for i from 0 to range - 1: for j from 0 to length(holes[i]) - 1: A[idx] ← holes[i][j] idx ← idx + 1
JavaScript implementation
function pigeonholeSort(arr) { const n = arr.length; if (n <= 1) return arr; let min = arr[0]; let max = arr[0]; for (let i = 1; i < n; i++) { if (arr[i] < min) min = arr[i]; if (arr[i] > max) max = arr[i]; } const range = max - min + 1; const holes = Array.from({ length: range }, () => []); for (let i = 0; i < n; i++) { holes[arr[i] - min].push(arr[i]); } let idx = 0; for (let i = 0; i < range; i++) { for (let j = 0; j < holes[i].length; j++) { arr[idx++] = holes[i][j]; } } return arr; }
Pros and cons
Pros
- Fast O(n + N) execution.
- Maintains stable sorting behavior.
- Simpler implementation than Counting Sort as it avoids prefix sums.
- Highly efficient when values are dense.
Cons
- Not an in-place sort, requires auxiliary space for range N.
- Extremely memory-intensive if range N is large.
- Can only sort keys that can be mapped to indices.
When to use it
- Tight Integer Distribution: when sorting integer datasets where range $N$ matches size $n$.
- Simple Objects: ideal when keys are dense and stability is required.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid