Common sort Algorithm (d) (Cardinal order, bucket sort) __ Cardinal order

Source: Internet
Author: User
Tags array length pow

Related articles:

Common sort algorithm (0) (summary and comparison of various sorting algorithms)

Common sort algorithm (i) (bubble sort, insert sort)

Common sort Algorithm (ii) (select sort)

Common sort algorithm (c) (Quick Sort, merge sort, count sort)

Common sort algorithm (cardinal order, bucket sort)


cardinality sort (radix sort)

Cardinality sorting is divided into lowest bit prioritization (leastsignificant Digit First, LSD) and highest bit prioritization (Most significantdigit First, MSD), which are different in the sorting method and the applicable data.

lowest-bit prioritization (least significant Digit first, LSD)

Starting at the lowest bit, each bit is mapped to the corresponding array by a hash function, and after the traversal, all the numbers are assigned to the new array, and the 10 arrays are spliced directly from top to bottom in the order of distribution. And then start with the penultimate bit, and when you go to the first place, you get the sorted array after the merge. Because the less the number of maps to the same array is, the better, so use h (x) = x 10 as the hash function.

Basic ideas:

1. Find the longest number in the array to be sorted, and set the length to L

2, establish a two-dimensional array data[10][n],n for the length of the array to be sorted, set up a counting array counts[10]

3, from the first place, each number assigned to the data array, according to Data[0]~data[9] in the order of merging

4, starting with 10 bits, assigning each number to the data array, merging in the order of Data[0]~data[9]

And so on, until you reach the top, you end up with a sorted array. Java implementation with lowest bit precedence:

int maxLength = string.valueof (Getmax ()). Length ();
Int[][] Radix;
int[] Index;
for (int i = 0; i < maxLength i++) {
    radix = new Int[10][data.length];
    index = new INT[10];
    Arrays.fill (index, 0);
    int divider = (int) math.pow (i);  Remove one
    //  allocation bucket for
    (int j = 0; J < Data.length; J + +) {
        int temp = data[j]
        by using divide-and-balance method; int key = Temp/divider%;  after the data in a bucket increases, the corresponding counter should be added 1
        radix[key][index[key] = temp;
        index[key]++;
    }  the data
    int counter = 0 in the stitching bucket;
    for (int j = 0; J < + j) {for
        (int k = 0; k < index[j]; k++) {
            Data[counter] = radix[j][k];
            counter++
        }}
    }

top priority ordering (Most significant Digit first, MSD)

The data is allocated to the bucket from the highest bit, unlike the lowest bit precedence, instead of merging immediately after the assignment, the data in the bucket is redistributed to the child bucket based on the second digit value, and then the data in the bucket is allocated to the child bucket based on the third digit value, until the last one is assigned to the merge. By the description of the algorithm, the algorithm occupies a large space, if the barrel distribution parallel propulsion, it is easy to cause memory shortage. Therefore, a recursive method is used to execute the algorithm. The hash function used is still h (x) = x 10.

Basic ideas:

1. Find the maximum length of the values in the array to be sorted, set to L

2, from the first position, through the hash function to allocate data to the bucket, L decrement

3, if L is greater than or equal to 0, repeat the second step, if L is less than 0, the description has been assigned to the last one, the array of each bucket is spliced and then returns the highest-ranked Java implementation:

int maxLength = string.valueof (Getmax ()). Length ();
int divider = (int) Math.pow (ten, maxLength-1);
data = Msditerationsort (data, divider);
/** * Iterative MSD sort * @param data to be sorted array * @param divider is used to fetch the divisor of the digits * @return * * Private int[] Msditerationsort (int[) data,
    int divider) {int[][] radix = new Int[10][data.length];
    int[] index = new INT[10];
    Arrays.fill (index, 0);
    int sum = 0;
        Assign the number to the corresponding bucket for (int j = 0; J < Data.length; J + +) {int pos = data[j]/divider% 10;
        Radix[pos][index[pos]] = data[j];
    index[pos]++;
    //Get the divisor divider = DIVIDER/10 for the next iteration;
            If the divisor is greater than 0, then the next iteration of the data in the bucket is required (Divider > 0) {//For each bucket to be iterated again for (int i = 0; i < i++) { The bucket size needs to be processed because the length of the bucket is the length of the data to be processed,//A new array needs to be created to store the data in the original bucket, and then used for the next iteration int[] temp = new
            Int[index[i]];
            for (int j = 0; J < Index[i]; J +) {Temp[j] = radix[i][j];
        } Radix[i] = Msditerationsort (temp, divider);
}//divisor is not greater than 0 when each bit of the log is processed, then the data in each bucket is pieced together to get the sorted data//And then return the sorted data.    for (int i = 0; i < i++) {sum = sum + index[i];
    } int[] result = new Int[sum];
    int counter = 0; for (int i = 0; i < i++) {for (int j = 0; J < Index[i]; J +) {Result[counter] = Radix[i][j
            ];
        counter++;
} return result; }

Cardinality ordering is also a sort algorithm that is not based on comparison and stability, and the optimal time complexity O (d (n + rd)), Worst time complexity O (d (r + N)), Average time complexity O (d (n + rd)), where R represents the cardinality of the keyword, d represents the length of the maximum data, and N represents the number of keywords. Space complexity O (rd + N) (the spatial complexity of uncertain lowest-bit priority and top-priority ordering is consistent, and the space complexity of feeling highest-priority order is higher)

Lowest-bit priority sorting is better suited to a situation where the data in the dataset has the highest number of digits, and when the data in the dataset has the highest number of digits, the highest bit priority is used. Cardinality sorting can only be sorted on the natural number, in the case of small amount of data but the span of large data range performance is better than the order of the count, occupy less space, in most cases, or the order of the count to be superior.

Bucket sort (Bucket sort)

Bucket sort, also called box sort, similar to cardinality sorting, the hash function is used to map data to a bucket, but unlike the cardinality order, when the number is mapped into a bucket, if two data enters the same bucket, the bucket is sorted so that the number in the bucket is ordered.

Basic ideas:

1, based on the hash function, the creation of two-dimensional array of data, number of rows is the number of cardinality, the number of columns for the length of the array to be sorted

2, traversing the array, put the number of the array into the corresponding bucket, if the bucket is used in the insertion sort, you can start sorting in this step, if the use of a quick sort, and so on after traversing the array and then sorting

3, after the order, the array of various arrays to get sorted out of the sequence of the array bucket sort of Java implementation (in the bucket using insert sort, the hash function is h (x) = x% 10):

Bucket = new Int[10][data.length];
index = new INT[10];
Arrays.fill (index, 0);
int divider = (int) Math.pow (string.valueof (Getmax ()). Length ()-1);
for (int i = 0; i < data.length i++) {
    int pos = data[i]/divider;  after putting the new values in the bucket, the data in the bucket becomes unordered,
    //Because the rest of the numbers  are ordered except for the last number, so consider using the insert sort to reorder the data in the bucket
    bucket[pos][index[ POS]] = data[i];
    index[pos]++;
    if (Index[pos] <= 1) {  //  bucket with only one data, do not need to insert sort, continue looping
        continue;
    }  Basic Insert sort for
    (int j = Index[pos]-1 J > 0; j--) {
        if (Bucket[pos][j] < bucket[pos][j-1]) {
            Swap (J, J-1, Bucket[pos]);
        else {break
            ;
        }
}} int counter = 0;
for (int i = 0; i < i++) {
    counter = Getbucketresult (i, counter);
}
/**
 * takes out the data in the bucket and returns the location where the next data should be placed
 * @param the number of the Pos bucket *
 @param counter the next place where data should be stored
 * @return
 *
private int getbucketresult (int pos, int counter) {for
    (int i = 0; i < Index[pos]; i++) {
        Data[cou Nter] = Bucket[pos][i];
        counter++;
    }
    return counter;
}

The optimal time complexity for bucket ordering is O (n), and the worst time complexity depends on the sort algorithm in the bucket, or O (N2) for the insertion sort, and the heap sort O (Nlogn).        The space complexity is O (d + N), D is the cardinality number, and n is the array length to be sorted. To improve the efficiency of the bucket order to reduce the data in each bucket, limit the case of each bucket only one data, time complexity can be reached O (n), but will waste a lot of space, which is the trade-off between time and space. In the case where the data scale is appropriate, the performance of the bucket sort can be better than the fast-ranked sort algorithm based on the comparison, because bucket ordering is not a comparison based sort algorithm.

The Java code used in this article has been uploaded to GitHub for the Java project:https://github.com/sysukehan/sortalgorithm.git


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.