Sorting algorithm (eight)--Cardinal sort

Source: Internet
Author: User
Tags pow

Basic ideas

The cardinality sort (Radix sort) is developed on the basis of bucket ordering, both of which are advanced implementations of allocation ordering. The basic idea of assigning sorting (distributive sort) is that the ordering process does not need to compare keywords , but rather through the assign and collect processes to achieve sorting. Their time complexity can reach the linear Order: O (n).

First look at the bucket sort (Radix sort).


Bucket sort is also called box sort (bin sort), the basic idea is: set several buckets, sequentially scan the records to be sorted r[0],r[1],...,r[n-1], the keywords in a certain range of records are loaded into the K-bucket (allocation), The non-empty buckets are then concatenated (collected) sequentially by ordinal.

For example, to a mix of 52 cards sorted by the number of a<2<...<j<q<k, you need to set 13 "barrels", sorting each card by the number of points into the corresponding bucket, and then sequentially to the end of the bucket, and then get the number of points ascending order of a deck of cards.

In bucket sorting, the number of buckets depends on the range of the keywords that are being evaluated. So bucket ordering requires that the type of the keyword is a finite type, otherwise an infinite bucket may be required.

In general, how many keywords are stored in each bucket is unpredictable, so the type of bucket should be designed as a linked list.

In order to ensure the order is stable, the allocation process during the packing and collection process must be in accordance with the FIFO principle.

For bucket ordering, the time of the allocation process is O (n), the time of the collection process is O (m) (the list is used to store the input to be sorted) or O (m+n). Therefore, the bucket sort time is O (m+n). If the number of buckets is O (n), the time of the bucket ordering is linear, i.e. O (n).

Some of the above-mentioned sorting algorithms, most of the time complexity are O (N2), there are some sorting algorithm time complexity is O (NLOGN). But the bucket sort can realize the time complexity of O (n). But the disadvantage of bucket sorting is: The first is the space complexity is higher, the additional overhead required. Sorting has two of the space cost of the array, one for the array to be sorted, and one is the so-called bucket, such as the value to be sorted from 0 to m-1, then need M bucket, this bucket array will be at least m space. Secondly, the elements to be sorted must be within a certain range and so on.

Cardinality sorting is an improvement to the sorting of buckets, which is the case of making the bucket sort fit for a larger set of element values rather than improving performance.

We still use the poker example to illustrate. A card has two keywords: color (Peach < Heart < plum < square) + face value (a<2<3<4<...<k). If the size of a card is first determined by the color, the same suit of cards have a number of decisions. We have two algorithms to solve this problem.

That is, two cards, if the color is different, regardless of the face value, the lower color of the card is less than the height of the color, only in the case of the same color, the size of the relationship is determined by the size of the face value. This is the multi-key ordering.

To get the sort results, we discuss two sorting methods.

Method 1: First the color sorting, divides it into 4 groups, namely Plum Blossom Group, block group, Hearts Group, Black Heart Group. Each group is then sorted by face value, and finally, the 4 groups are connected together.

Method 2: First give 13 numbered groups (2nd, 3rd, ..., A) by 13 denominations, and place the cards in the corresponding numbering group in denominations, divided into 13 piles. Then according to the color given 4 number group (plum, block, Hearts, Black Heart), the group of 2nd in the card out into the corresponding suit group, and then the group of 3rd of the cards are taken out into the corresponding suit group, ..., so that 4 suits are in order, and then, the 4 suit group is connected in turn.

Multi-key sorting is sorted in two ways: from the most-important-to-most-critical-to-most-key or from the first-to-most-primary-to-maximum-key-code sequence.

highest priority (most significant Digit first) method , or MSD method:

1) first sorted by K1 group, the sequence is divided into several sub-sequences, the same sequence of records, the key code k1 equal.

2) The groups are divided into subgroups by K2, after which the key codes continue to be grouped until the sub-groups are sorted by the most-important KD.

3) by connecting the groups together, an ordered sequence is obtained. The method of playing poker in the sort of suit and denomination is the MSD method.

Lowest bit priority (Least significant Digit first) method , hereinafter referred to as the LSD method:

1) Start with the KD first, then sort the kd-1, repeat until the K1 sorted by the smallest sub-sequence.

2) Finally, the various sub-sequences are connected, you can get an ordered sequence, poker by color, the value of the method described in the order of the second is the LSD method.

A single keyword of a numeric or character type can be thought of as a multi-keyword consisting of multiple digits or characters, which can be sorted by the method of "Allocation-collection", a process known as the Cardinal sort , where each number or character may be called the cardinality of the number of values . For example, poker's color base is 4, the face value base is 13. In the arrangement of playing cards, can be arranged according to the color, can also be sorted by face value first. According to the color arrangement, first by the red, black, square, flower order into 4 stacks (allocation), and then stacked together in this order (collection), and then in the order of face value divided into 13 stacks (allocation), and then stacked together in this order (collect), so two times allocated and collected can be arranged in order to arrange cards.

In the process of "allocation-collection", the stability of the ordering needs to be ensured.

The idea of a cardinal sort is to assign each group of keywords in the pending data sequentially to the bucket. For example, the following sequence of rows:

135, 242, 192, 93, 345, 11, 24, 19

We divide the digits of each value by 10 bits and the hundred into three keywords, for example:

135-K1 (single digit) =5, K2 (10-bit) =3, K3 (hundred) = 1.


Then start with the lowest bit digit (starting with the first keyword), k1 all the data to the bucket allocation (because each number is 0-9, so the bucket size is 10), and then output the data in the bucket to get the following sequence.

(11), (242, 192), (93), (24), (135, 345), (19) (starting from the first keyword, ignoring the hundred and 10 digits, and assigning them in digits)

Then the above sequence is then allocated to the bucket for K2, and the output sequence is:

(11, 19), (24), (135), (242, 345), (192, 93) (refer to the first keyword to sort the second keyword, ignoring the hundred and bits, according to the number assigned on 10 digits)

Finally, for the bucket allocation of K3, the output sequence is:

(011, 019, 024, 093), (135, 192), (242), (345) (refer to the second keyword to sort the highest keyword, ignore 10 bits and digits, and distribute according to the number on the hundred)

Sorting is complete.


Java implementation

public void Radixsort () {int max = array[0]; for (int i=0;i<array.length;i++) {//finds the maximum value in the array if (Array[i]>max) {max = a                    Rray[i];  }} int keysnum = 0; Number of keywords, we use digit, 10, hundred ...                    As the keyword, so the number of keywords is the maximum number of digits while (max>0) {max/= 10;             keysnum++; } list<arraylist<integer>> buckets = new Arraylist<arraylist<integ             Er>> ();  for (int i=0;i<10;i++) {//each digit may be 0~9, so set 10 buckets Buckets.add (New arraylist<integer> ()); The bucket is made up of arraylist<integer>} for (int i=0;i<keysnum;i++) {//by the first keyword, followed by the keyword                           with for (int j=0;j<array.length;j++) {//scan all array elements, assign elements to the corresponding buckets Remove the element corresponding to the number on the i+1 bit, such as 258, now to remove the 10 digits on the number, 258%100=58,58/10=5 int key =array[j]% (int) Math.pow (ten, i+1)/(int) Math.pow (ten, I);  Buckets.get (Key). Add (Array[j]);  After the element is placed in the bucket with the keyword key}//allocated, the elements in the bucket are copied back to the array int  Counter = 0; Element counter for (int j=0;j<10;j++) {arraylist<integer> bucket =BUCKETS.G  ET (j); Bucket with the keyword J while (Bucket.size () >0) {array[counter++] = Bucke  T.remove (0); Copy the first element in the bucket to the array and remove the}} System.out.print ("i+1" + "Round Order"):                    ");             Display (); }                  }

The printing results are as follows:



Algorithm analysis

At first glance, the execution efficiency of the cardinality sort seems so unbelievable that all you have to do is copy the original data items from the array to the list and then copy them back. If there are 10 data items, then there are 20 copies, which are repeated for each bit. Assuming that you sort the 5-bit numbers, you need to 20*5=100 the secondary copy. If there are 100 data items, then there is 200*5=1000 replication. The number of copies is proportional to the number of data items, which is O (n). This is the most efficient sorting algorithm we have seen.

Unfortunately, the more data items, the longer the keyword is required, and if the data item increases by 10 times times, the keyword must be incremented by one bit (one more round order). The number of copies and the number of data items is proportional to the length of the keyword, which can be thought of as the logarithm of N. So in most cases, the execution efficiency of the cardinality sort is reversed to O (N*logn), and the fast sort is almost.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sorting algorithm (eight)--Cardinal sort

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.