The Cardinal sort (Radix sort) is a non-comparative integer sorting algorithm that cuts the number of integers by bits into different numbers and compares them by each bit. Because integers can also express strings (such as names or dates) and floating-point numbers in a particular format, the cardinality sort is not only used for integers. The cardinality sequencing invention dates back to the 1887 Herman He Leri's contribution to the punch card watchmaking (tabulation machine).
It does this by unifying all the values to be compared (positive integers) to the same digit length, which is preceded by a short number of digits of 0. Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.
The cardinality is sorted by LSD (Least significant digital) or MSD (most significant digital), and LSD is sorted by the rightmost start of the key value, while the MSD is the opposite, starting with the leftmost side of the key value. The radix sort of LSD applies to the small number of digits, and the efficiency of using an MSD is better if the number of bits is more. MSD, in contrast to LSD, is assigned by the high number as the base, but does not merge back into an array immediately after the allocation, but instead creates a "sub-bucket" in each "bucket", assigning the value in each bucket to the "sub-bucket" in terms of the next digit. Once the minimum number of digits has been allocated, it is merged back into a single array.
This article address: http://www.cnblogs.com/archimedes/p/radix-sort-algorithm.html, reprint please indicate source address.
ExampleTake LSD, for example, assuming that a string of values is as follows: 73, 22, 93, 43, 55, 14, 28, 65, 39, 81
The first step
First, based on the numeric values of single digits, they are assigned to buckets numbered 0 to 9 when they are visited: 01 812 223 7393434 145 5565678 289 39
Step Two
The values in these buckets are then re-strung to form the following sequence: 81, 22, 73, 93, 43, 14, 55, 65, 28, 39 and then once again, this time being allocated according to the 10-digit number: 011422228339443555665773881993
Step Three
The values in these buckets are then re-threaded to become the following sequence: 14, 22, 28, 39, 43, 55, 65, 73, 81, 93 this time the whole sequence is sorted out;if the sorted object has more than three digits, the above action continues until the maximum number of digits。 The algorithm implementation of the following code from the wiki, the original code is described in C + +, here to C description, add some comments to understand, because the code is very subtle, it is worth learning
//completed on 2014.10.10 21:10//Language:c99////Copyright (C) Codingwu (mail: [email protected])//Blog Address:http://www.cnblogs.com/archimedes/#include <stdio.h>#include<stdlib.h>intMaxbit (intData[],intN//Auxiliary function to find the maximum number of digits in the data{ intD =1;//Save the maximum number of digits intp =Ten; for (int i = 0; i < n; + +i) { while (Data[i] >= p) { P *= ten; + +D; } } returnD;}voidRadixsort (intData[],intN//Base Sort{ intD = maxbit (data, n);//The maximum number of digits in an array of elements int*tmp = (int*) malloc (n *sizeof(int)); int*count = (int*) malloc (Ten*sizeof(int));//counter intI, j, K; intRadix =1; for(i =1; I <= D; i++) {//to sort D for(j =0; J <Ten; J + +) Count[j]=0;//empty counters before each allocation for(j =0; J < N; J + +) { k = (Data[j]/radix)% ; //calculates the number of a bit per loopcount[k]++;//Count the number of records in each bucket } for(j =1; J <Ten; J + +) Count[j] = count[j- 1] + count[j]; //the total number of elements in the first J bucket and all previous buckets For (j = n- 1; J >= 0; j--) { // to collect all the records in the bucket in turn to the tmp k = (da TA[J]/radix)% ; Tmp[count[k] - 1] = Data[j]; COUNT[K]-- ; } for(j =0; J < N; J + +)//Copy the contents of the temporary array to dataDATA[J] =Tmp[j]; Radix= Radix *Ten; } free (TMP); Free (count);}intMain () {intA[] = {288, the,123, -,212, at,Ten,233}; intN; N=sizeof(a)/sizeof(a[0]); Radixsort (A, n); for(intK =0; K < n; k++) printf ("%d", A[k]); printf ("\ n"); return 0;}
Efficiency Analysis
Time efficiency: Set to be sorted as N records, D key Code, key code value range is radix, then the time complexity of chain base sorting is O (d (N+radix)), wherein, a trip allocation time complexity is O (n), a trip collection time complexity of O (Radix), A total of D-trip allocations and collections.
Space efficiency: You need to 2*radix a secondary space pointing to the queue and N pointers for static lists.
Cardinality sorting algorithm