Pigeonhole sort is an interesting sorting algorithm that makes use of an array filled with zeros and its indices. It can sort values in linear time and is relatively simple to implement. However, the tradeoff for its sorting speed is the space that it requires. Also, since it makes use of array indices, only positive integers can be sorted using this algorithm.
So the basic idea of pigeonhole sort is:
- Create a new array (let’s call it pigeonholeArray) and fill it with zeros.
- Iterate through the unsorted user’s array (inputArray).
- For each value of the inputArray, add 1 to the index corresponding with the value in the pigeonholeArray.
Let’s say for this example, we want to sort this inputArray:
// inputArray:
[5, 2, 7, 5, 1]
The first thing the algorithm needs to know is what the maximum value of the inputArray is. We need to create a pigeonholeArray that has indices that go up to the max value. If we don’t know the maximum, we’ll have to loop through the inputArray one time to figure it out. In this case, our biggest value is 7, thus we need to create a pigeonholeArray that can hold 8 elements (which will have indices 0 to 7).
// pigeonholeArray:
[0, 0, 0, 0, 0, 0, 0, 0]
Now we need to loop through the unsorted inputArray (again), and as we do that, we will add 1 to the pigeonholeArray’s value at the pigeonholeArray’s index corresponding with the inputArray’s value. So as we iterate our input array, our pigeonhole array will look like this:
// Progress of pigeonholeArray
[0, 0, 0, 0, 0, 1, 0, 0] // when the inputArray value is at 5
[0, 0, 1, 0, 0, 1, 0, 0] // when the inputArray value is at 2
[0, 0, 1, 0, 0, 1, 0, 1] // when the inputArray value is at 7
[0, 0, 1, 0, 0, 2, 0, 1] // when the inputArray value is at 5
[0, 1, 1, 0, 0, 2, 0, 1] // when the inputArray value is at 1
As you can see, after looping through the entire inputArray, our pigeonholeArray will contain all of inputArray’s values. We will just have to make one last iteration through the pigeonholeArray. For each pigeonholeArray’s value that is greater than zero, we’ll add the pigeonholeArray’s index to the inputArray. We can replace the old values in the inputArray so that no new array needs to be created.
Check out the completed algorithm below:
function pigeonholeSort(inputArray) {
// Find the max value in inputArray
var max = inputArray[0];
for (var i = 1; i < inputArray.length; i++) {
if (max < inputArray[i])
max = inputArray[i];
}
// Create pigeonholeArray filled with zeros
var pigeonholeArray = [];
for (var i = 0; i <= max; i++) {
pigeonholeArray.push(0);
}
// Iterate through inputArray and add 1's to pigeonholeArray
for (var i = 0; i < inputArray.length; i++) {
pigeonholeArray[inputArray[i]]++;
}
// Iterate through the pigeonholeArray and replace values in inputArray
var inputArrayIndex = 0;
for (var i = 0; i < pigeonholeArray.length; i++) {
// Add to inputArray until the current pigeonholeArray index is 0
while (pigeonholeArray[i]) {
inputArray[inputArrayIndex] = i;
pigeonholeArray[i]--;
inputArrayIndex++;
}
}
// return sorted inputArray
return inputArray;
}