Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements and the number of possible key values are approximately the same. It requires O(n + Range) time where n is number of elements in input array and ‘Range’ is number of possible values in array. Working of Algorithm :
Find minimum and maximum values in array. Let the minimum and maximum values be ‘min’ and ‘max’ respectively. Also find range as ‘max-min+1’.
Set up an array of initially empty “pigeonholes” the same size as of the range.
Visit each element of the array and then put each element in its pigeonhole. An element arr[i] is put in hole at index arr[i] – min.
Start the loop all over the pigeonhole array in order and put the elements from non- empty holes back into the original array.
Comparison with Counting Sort : 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 “.
/* C program to implement Pigeonhole Sort */
/* Sorts the array using pigeonhole algorithm */
// Find minimum and maximum values in arr
intmin = arr, max = arr;
for(inti = 1; i < n; i++)
if(arr[i] < min)
min = arr[i];
if(arr[i] > max)
max = arr[i];
intrange = max - min + 1; // Find range
// Create an array of vectors. Size of array
// range. Each vector represents a hole that
// is going to contain matching elements.
// Traverse through input array and put every
// element in its respective hole
for(inti = 0; i < n; i++)
// Traverse through all holes one by one. For
// every hole, take its elements and put in
intindex = 0; // index in sorted array
for(inti = 0; i < range; i++)
for(it = holes[i].begin(); it != holes[i].end(); ++it)