![]() |
VOOZH | about |
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 :
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 ".
Sorted order is : 2 3 4 6 7 8 8
Pigeonhole sort has limited use as requirements are rarely met. For arrays where range is much larger than n, bucket sort is a generalization that is more efficient in space and time.
Complexity Analysis of Pigeonhole Sort:
The time complexity of Pigeonhole Sort is O(n + range), where n is the number of elements in the array and range is the range of the input data (i.e., the difference between the maximum and minimum values in the array).
In the given implementation, the algorithm first finds the minimum and maximum values in the array, which takes O(n) time. Then, it calculates the range, which takes constant time. Next, it creates an array of vectors of size equal to the range, which takes constant time. Then, it traverses the input array and puts each element into its respective hole, which takes O(n) time. Finally, it traverses all the holes and puts their elements into the output array in order, which takes O(range) time.
Therefore, the overall time complexity of the algorithm is O(n + range). In the worst case, when the range is significantly larger than the number of elements in the array, the algorithm can be inefficient. However, it can be useful for sorting integer arrays with a relatively small range.
Auxiliary Space: O(range)
Advantages of Pigeonhole sort:
Disadvantages of Pigeonhole sort:
References:
https://en.wikipedia.org/wiki/Pigeonhole_sort
This article is contributed Ayush Govil.
Other Sorting Algorithms on GeeksforGeeks/GeeksQuiz
Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Heap Sort, QuickSort, Radix Sort, Counting Sort, Bucket Sort, ShellSort, Comb Sort,