The Cardinal sort is so simple.

Source: Internet
Author: User

First, the base sort (bucket sort) Introduction

SOURCE 36,000 section:

The radix sort (radix sort) belongs to the "distributive sort" (distribution sort), also known as the "bucket method" (bucket sort) or bin sort, as the name implies, it is a part of the information through the key value, the elements to be sorted into some "barrels", In order to achieve the role of sequencing, the cardinal ranking method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the base taken, and M is the number of heaps, at some point, the cardinality of the sorting method is more efficient than other stability ranking method.

From the simple introduction above, it is not understood how the cardinal sort is made-the base sort differs from the other 7 sorts, the other 7 sorts are sorted by the exchange or the comparison, but the cardinal sort is not, it is according to the allocation, recycles (distributes to the different location, then recycles). Constant distribution: Recycle to sort until ordered.

It sounds like a big, hard look, but it's not. The Cardinal sort is quite simple, and I'll take a look at the cardinal sort flow ....

We have 9 buckets, and the numbers in the array are allocated in the buckets as values:

PS: Image from the network, invasion and deletion

Above we found: if the bucket is recycled sequentially , then our sort is done ~

However, in general, our array elements are not only single digit number, then the number of high-level number how to do it?? For example: 23,44,511,6234 These high-level numbers.

In fact, the same is true:

    • The first bucket sort assigns a single digit of the number to the bucket and then recycles it, and all the digits of the array element are sorted in order.
    • The second bucket sort assigns the 10 digits of the number to the bucket, and then recycles it, and all the single and 10 digits of the array element are sorted (if there are No 10 digits, then 0)
    • The third bucket sort distributes the hundreds of numbers into buckets, and then recycles them, all the single-digit and 10-digit and hundred- digit elements of the array are sorted (if no hundred, then 0)
    • ..................................
    • Until all the numbers (a, ten, Hundred, Thousand ...) In order, the array is ordered.

PS: Image from the network, invasion and deletion

Wit's classmates may find out, about this bucket we can use a two-dimensional array to store.

10 barrels is 10 columns, if the allocation of the same number, then make a number of lines ~

Second, the base sequencing experience

First we have the following array:

   int[] arrays = {6,  432243234455 };

Now we have 10 barrels, each of which can load arrays.length a number.

intnewint[arrays.length][10];

The effect is as follows:

0 1 2 3 4 5 6 7 8 9
2.1 First trip allocation and recycling

Assigns each single digit of an array to a different bucket:

/tr> td>
0 1 2 3 4 5 6 7 8 9
6
4322
432
344
+

After allocation, we recycle in order: The result should be this:{4322,432,344,55,6}

2.2 Section two allocation and recycling

Assign each 10-bit number of the array to a different bucket (like 6 for a number, 0 for the front):

So we can get this sort of:

/tr> td>
0 1 2 3 4 5 6 7 8 9
6
4322
432
344
+

After allocation, we recycle in order: The result should be this:{6,4322,432,344,55}

2.3 Section three allocation and recycling

Assign each hundred of the array to a different bucket (such as 6, 55, to the front 0):

So we can get this sort of:

td> td>
0 1 2 3 4 5 6 7 8 9
6
up 4322
432
344

After allocation, we recycle in order: The result should be this:{6,55,4322,344,432}

2.3 Section four allocation and recycling

Assign each hundred of the array to a different bucket (such as 6, 55,344,432, to the front 0):

So we can get this sort of:

0 1 2 3 4 5 6 7 8 9
6
up
344 /td>
432 /td>
4322

After allocation, we recycle in order: The result should be this:{6,55,344,432,4322}

At this point our array can already be sorted out ~ ~ ~ The process is such a child, in fact, it is not difficult to have only two steps:

    • Put every bit of the array in the bucket
    • Recovery
    • Cycle......
Third, the base sorting code to write

Our cardinality is sorted by one, ten, hundred, Thousand ... To be stored. The previous demo is a case where the data of the array elements has been known, but generally we ignore the values of the elements in the array. How do we deal with this condition if the number of bits is a lot (million) or is a single digit?

We can do this: first of all, the maximum value of the array, and then continuously/10, as long as it can be greater than 0, then its number of bits:

3.1 Find the maximum value of the array

I have this code when I write recursion in front of me, and I move directly to the recursive code, and by the way, review it:

    • Of course, it's better to just use the For loop to find out (readability is better)
    /*** Recursive, find the maximum value of the array     * @param arraysArray     * @param Lleft border, first number     * @param Rright boundary, length of array* @return     */     Public Static int Findmax(int[] Arrays,intLintR) {//If the array has only one number, then the largest is the first value of the array        if(L = = R) {returnARRAYS[L]; }Else{intA = Arrays[l];intb =Findmax(Arrays, L +1, R);//Find out the overall maximum value            if(A > B) {returnA }Else{returnb }        }
3.2 Code Implementation
     Public Static void Radixsort(int[] arrays) {intMax =Findmax(Arrays,0, arrays.length-1);//The number of iterations that need to be traversed is determined by the number of bits of the array's maximum value         for(inti =1; max/i >0; i = i *Ten) {int[] Buckets =New int[Arrays.length][Ten];//Get every digit (one, ten, Hundred, Thousand ...) Assigned to a bucket)             for(intj =0; J < Arrays.length; J + +) {intnum = (Arrays[j]/i)%Ten;//Put it in a bucketBuckets[j][num] = Arrays[j]; }//Recycle the elements in the bucket            intK =0;//with 10 barrels             for(intj =0; J <Ten; J + +) {//Recycle the elements in each bucket                 for(intL =0; L < arrays.length; l++) {//If there are elements in the bucket, it is recycled (data initialization will be 0)                    if(Buckets[l][j]! =0) {arrays[k++] = buckets[l][j]; }                                    }                            }        }    }

Got a bunch of numbers tested one ha:

Iv. Summary

The cardinality sort (bucket sort) is not difficult to understand, but it is worth noting that the cardinality sort is difficult to sort by a number of numbers and 0

    • Therefore, there are usually 0 and negative arrays that we do not use cardinality to sort

The main points of the Cardinal order are two:

    • Allocation: In different buckets according to the size of the element
    • Recycle: Re-place the elements in the bucket into the array in the bucket order
    • Repeat..... Two steps

Resources:

    • Http://www.cnblogs.com/skywang12345/p/3603669.html
    • Http://www.cnblogs.com/developerY/p/3172379.html
    • Https://www.cnblogs.com/zengzhihua/p/4456753.html
    • Https://www.cnblogs.com/jin-nuo/p/5293554.html
    • Https://www.cnblogs.com/protected/p/6603536.html
    • Https://www.cnblogs.com/Braveliu/archive/2013/01/21/2870201.html

If the article is wrong, welcome to correct, we communicate with each other. accustomed to look at technical articles, want to get more Java resources of students, can pay attention to the public number: Java3y

The Cardinal sort is so simple.

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.