Pigeon Nest Sort pigeonhole sort

Source: Internet
Author: User
The Pigeon Nest Sort, also known as Cardinal classification, is a sort algorithm with time complexity (Theta (n)) that is the most efficient when it is unavoidable to traverse each element and sort. However, it is useful only if the difference (or the number of values that can be mapped in the difference) is sorted in a small range. When multiple unequal elements are involved and the elements are placed in the same "pigeon nest", the efficiency of the algorithm is reduced. In order to be simple and keep the pigeon nest sorted in different situations, such as two elements that end in the same bucket must be equal.

We generally rarely use pigeon nests for sorting because it is rarely available in flexibility, simplicity, and especially speed over other sorting algorithms. In fact, the bucket sort is more practical than the pigeon-nest sort. A more famous variant of the pigeon nest sort is the tally sort, which only applies to very limited topics, and this algorithm is famous in programming pearls as an example of a method for solving a very limited set of problems. Clearly, a quick sort can be used as a sort of pigeon nest with only two (in some cases three) "Pigeon nests".


algorithm Efficiency
Worst time complexity: O (N+n)
Best time complexity: O (N+n)
Average time complexity: O (N+n)
The worst of the space complexity: O (N*n)


Algorithm Analysis 1. Given a sorted array, create an alternate array (pigeon nest) and initialize the element to 0, and the index of the alternate array is the value of the array to be sorted.
2. Place the value of the array to be sorted into the "pigeon Nest" (that is, the index used as an alternate array).

3. The values in the pigeon nest are then sent back to the sorted array.

Pigeon Nest Sort///</summary>///<param name= "unsorted" > Backlog array </param>///<param na Me= "MaxNumber" > Maximum number of arrays to be ranked, if specified </param>///<returns></returns> static int[] Pogeon
            _sort (int[] unsorted, int maxnumber = ten) {int[] Pogeonhole = new Int[maxnumber + 1];
            foreach (var item in unsorted) {pogeonhole[item]++;
            return pogeonhole; * * POGEONHOLE[10] = 4;
            The implication is that there are 4 10 occurrences in the array to be ranked, the same as other/} static void Main (string[] args) {
            int[] x = {99, 65, 24, 47, 47, 50, 99, 88, 66, 33, 66, 67, 31, 18, 24};
            var sorted = Pogeon_sort (x, 99); for (int i = 0; i < sorted. Length; i++) {for (int j = 0; J < Sorted[i]; J +) {CONSOLE.WR
                Iteline (i);
            }} console.readline (); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.