Science Fair Projects Ideas - Pigeonhole sort

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Pigeonhole sort

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 range must be limited to some constant times the number of elements that is being sorted to achieve the linear running time. Pigeonhole sort orders the elements by the result of this mapping.

The algorithm works as follows.

  1. Set up an array of initially empty "pigeonholes" the size of the range.
  2. Go over the original array, putting each object in its pigeonhole.
  3. Iterate over the pigeonhole array in order, and put elements from non-empty holes back into the original array.

The hidden constant for this algorithm depends critically on the density of the elements in the pigeonhole array. If there are many more array elements than items to be sorted, steps 1 and 3 will be relatively slow.

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.

09-23-2007 01:00:40
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice