Pigeonhole sort Guide, Meaning , Facts, Information and Description
Pigeonhole sorting takes linear time (O(n)), which is the best possible performance for a sort algorithm since one must inevitably look at each of the elements being sorted at least once, regardless of the sorting algorithm. However, it requires- that no two objects in the array be identical;
- an invertible function mapping the objects you are sorting to integers within a small range (say, 0 to 1000).
The algorithm works as follows.
- Set up an array of initially empty "pigeonholes" the size of the range.
- Go over the original array, putting each object in its pigeonhole.
- Iterate over the pigeonhole array in order, and put elements from non-empty holes back into the original array.
Pigeonhole sort is rarely used as the requirements are rarely met and other, more flexible, and almost as fast, sorting algorithms are easier to use. In particular, the bucket sort is a more practical variation on the pigeonhole sort.
Sample C99 code for this algorithm:
void pigeonhole_sort(int *low, int *high, int min, int max)
{
int count, *current;
const int size = max - min + 1;
bool holes[size];
/* Populate the pigeonholes. */
for (current = low; current <= high; current++)
holes[*current - min] = true;
/* Put the elements back into the array in order. */
for (current = low, count = 0; count < size; count++)
if (holes[count])
*current++ = count + min;
}
Note that min and max could also easily be determined within the function.
This is an Article on Pigeonhole sort. Page Contains Information, Facts Details or Explanation Guide About Pigeonhole sort
