Skip to main content

Pigeonhole Sort


Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm.

The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole.

Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence.

Pigeonhole sort has a time complexity of O(n+k), where n is the number of elements to be sorted and k is the range of values in the input sequence. This makes it a relatively fast sorting algorithm for small ranges of values, but it can become inefficient for large ranges of values or if the number of elements to be sorted is much larger than the range of values.

Pigeonhole sort can be used to sort a variety of data types, including integers, floating-point numbers, and strings. However, it is generally not suitable for sorting complex data structures such as arrays of structures or objects.

One potential disadvantage of pigeonhole sort is that it requires the entire range of values to be known in advance. This can make it impractical for situations where the range of values is not known or is very large.

In summary, pigeonhole sort is a simple and efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Its time complexity is O(n+k), and it can be used to sort a variety of data types. However, it may not be suitable for sorting large ranges of values or complex data structures.

ALGORITHM:

The steps for Pigeonhole Sort are as follows:
  1. Find the minimum and maximum values in the input sequence.
  2. Calculate the range of values as (max - min + 1).
  3. Create an array of empty "pigeonholes" equal to the range of values.
  4. Iterate through the input sequence and place each element in its corresponding pigeonhole based on its value.
  5. Iterate through each pigeonhole in order and place the elements back into the original sequence.
  6. The original sequence is now sorted.
Here's a more detailed explanation of each step:
  1. Find the minimum and maximum values in the input sequence. This can be done by iterating through the sequence and keeping track of the minimum and maximum values found so far.
  2. Calculate the range of values as (max - min + 1). This represents the number of pigeonholes needed to sort the sequence.
  3. Create an array of empty "pigeonholes" equal to the range of values. This can be done using a simple array of size range.
  4. Iterate through the input sequence and place each element in its corresponding pigeonhole based on its value. For example, if the element is x, then it is placed in the pigeonhole at index x - min.
  5. Iterate through each pigeonhole in order and place the elements back into the original sequence. This involves iterating through the pigeonhole and adding each element to the output sequence.
  6. The original sequence is now sorted.

PSEUDO CODE:

Here's the pseudocode for Pigeonhole Sort:



This pseudocode assumes that the input array contains integers. The algorithm first finds the minimum and maximum values in the array to determine the range of values. It then creates an array of empty pigeonholes equal to the range of values. Each element in the input array is placed in its corresponding pigeonhole based on its value. Finally, the elements are put back into the original array by iterating through the pigeonholes in order and adding each element to the output array.

PYTHON CODE:

Here's an implementation of Pigeonhole Sort in Python:



This implementation takes an input array and returns a sorted array using Pigeonhole Sort. It first finds the minimum and maximum values in the array to determine the range of values. It then creates an array of empty pigeonholes equal to the range of values. Each element in the input array is placed in its corresponding pigeonhole based on its value. Finally, the elements are put back into the original array by iterating through the pigeonholes in order and adding each element to the output array.

JAVA CODE:

Here's an implementation of Pigeonhole Sort in Java:



This implementation takes an input array and returns a sorted array using Pigeonhole Sort. It first finds the minimum and maximum values in the array to determine the range of values. It then creates an array of empty pigeonholes equal to the range of values. Each element in the input array is placed in its corresponding pigeonhole based on its value. Finally, the elements are put back into the original array by iterating through the pigeonholes in order and adding each element to the output array.

Note that this implementation uses an array of Lists to represent the pigeonholes. This is because the number of elements in each pigeonhole is not known in advance.

C CODE:

Here's an implementation of Pigeonhole Sort in C:



This implementation takes an input array and its size, and sorts the array in place using Pigeonhole Sort. It first finds the minimum and maximum values in the array to determine the range of values. It then creates an array of empty pigeonholes equal to the range of values, using dynamic memory allocation with calloc(). Each element in the input array is placed in its corresponding pigeonhole based on its value. Finally, the elements are put back into the original array by iterating through the pigeonholes in order and adding each element to the output array.

Note that this implementation uses calloc() to allocate memory for the pigeonholes array, and free() to deallocate it when it is no longer needed.

C++ CODE:

Here's an implementation of Pigeonhole Sort in C++:



This implementation takes an input vector and sorts it in place using Pigeonhole Sort. It first finds the minimum and maximum values in the vector to determine the range of values. It then creates a vector of empty pigeonholes equal to the range of values. Each element in the input vector is placed in its corresponding pigeonhole based on its value. Finally, the elements are put back into the original vector by iterating through the pigeonholes in order and adding each element to the output vector.

Note that this implementation uses vector to represent the pigeonholes, and the push_back() function is not used to add elements to the pigeonholes because their size is known in advance. Instead, the operator[] is used to access and modify the elements of the vector.

JAVASCRIPT CODE:

Here's an implementation of Pigeonhole Sort in JavaScript:



This implementation takes an input array and sorts it in place using Pigeonhole Sort. It first finds the minimum and maximum values in the array to determine the range of values. It then creates an array of empty pigeonholes equal to the range of values using the fill() method. Each element in the input array is placed in its corresponding pigeonhole based on its value. Finally, the elements are put back into the original array by iterating through the pigeonholes in order and adding each element to the output array.

Note that this implementation uses let and const to declare variables instead of var to prevent scoping issues, and the join() method is used to convert the arrays into strings for display purposes.

Comments

Popular posts from this blog

Selection Sort

Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array. The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays. One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations. Another advantage of selection sort is that it is a stable so

Counting Sort

Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output. The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array. Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times. The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k