Radix sort analysis + code

Radix sort  is reproduced from http://www.cnblogs.com/jingmoxukong/p/4311237.html

Radix sort is different from the usual insertion sort method, it does not need to compare the size of the keys .

It is based on the value of each bit in the keyword, by performing several times of "allocation" and "collection" on the sorted N elements to achieve sorting. 

 

Let's take a concrete example to show how radix sort works. 

Let an initial sequence be: R { 50, 123, 543, 187, 49, 30, 0, 2, 11, 100 }.

We know that for any Arabic number, the bases of its digits are represented by 0~9.

So we might as well treat 0~9 as 10 buckets. 

We first classify the sequence according to the single digit number and divide it into the specified bucket. For example: R[0] = 50, the single digit is 0, and this number is stored in the bucket numbered 0.

After classification, we take these numbers out of each bucket in the order from number 0 to number 9.

At this time, the obtained sequence is a sequence with an increasing trend in single digits. 

Sort by single digit: {50, 30, 0, 100, 11, 2, 123, 543, 187, 49}.

Next, the tens and hundreds digits can also be sorted in this way, and finally the sorted sequence can be obtained.

Analysis of Algorithms

Radix sort performance

Sort category

Sort method

time complexity

space complexity

stability

Complexity

Average situation

worst case

best case

radix sort

radix sort

O(d(n+r))

O(d(n+r))

O(d(n+r))

O(n+r)

Stablize

more complicated

 time complexity

As can be seen from the above, it is assumed that in the radix sort, r is the radix and d is the number of digits. Then the time complexity of radix sort is O(d(n+r)) .

We can see that the efficiency of radix sorting has nothing to do with whether the initial sequence is ordered.

 space complexity

During radix sort, n+r temporary space is required for "bucketing" the radix on any number of digits.

 Algorithm Stability

In the radix sorting process, the elements with the same value in the current number of digits are uniformly "bucketed" each time, and there is no need to exchange positions. So radix sort is a stable algorithm.

Code:

public class RadixSort {
	public int getDigit(int x, int d) {
        int a[] = {
                1, 1, 10, 100
        }; //The first one is not used, you can give as much as you want, the insurance is 1, the maximum number in this example is hundreds, so as long as it reaches 100
        return ((x / a[d]) % 10);
        //The value obtained by 187/10 is 18, so take the remainder of 10 to get the value in the tens place
    }
 
    public void radixSort(int[] list, int begin, int end, int digit) {
        final int radix = 10; // radix, defined as constant
        int i = 0, j = 0;
        int[] count = new int[radix]; // Store the statistics of each bucket, 10 buckets
        int[] bucket = new int[end - begin + 1];//Number in list
 
        // Execute the sorting process in order from low to high
        for (int d = 1; d <= digit; d++) {
 
            // Empty the data statistics of each bucket
            for (i = 0; i < radix; i++) {
                count[i] = 0;
            }
 
            // Count the number of data to be loaded into each bucket
            for (i = begin; i <= end; i++) {
                j = getDigit(list[i], d);
                count[j]++;
            }//The corresponding bucket count++, here is just the number of each bucket will be put
 
            // count[i] represents the index of the right boundary of the ith bucket
            for (i = 1; i < radix; i++) {
                count[i] = count[i] + count[i - 1];
            }
 
            // Load data into buckets in turn
            // Scan from right to left here to ensure sorting stability
            for (i = end; i >= begin; i--) {
                j = getDigit(list[i], d); // Find the number of the kth digit of the key code, for example: the 3rd digit of 576 is 5
                bucket[count[j] - 1] = list[i]; // put into the corresponding bucket, count[j]-1 is the index of the right boundary of the jth bucket
                count[j]--; // The index of the loaded data of the corresponding bucket is decremented by one
            }
 
            // Pour out the data in the allocated bucket, at this time it is a table corresponding to the current number of digits
            for (i = begin, j = 0; i <= end; i++, j++) {
                list[i] = bucket[j];
            }
        }
    }
 
    public int[] sort(int[] list) {
        radixSort(list, 0, list.length - 1, 3);
        return list;
    }
 
    // print the complete sequence
    public void printAll(int[] list) {
        for (int value : list) {
            System.out.print(value + "\t");
        }
        System.out.println();
    }
 
    public static void main(String[] args) {
        int[] array = {
                5, 23, 534, 187, 49, 30, 0, 21, 1, 100
        };
       
        RadixSort radix = new RadixSort();
        System.out.print("Before sorting:\t\t");
        radix.printAll(array);
        radix.sort(array);
        System.out.print("After sorting:\t\t");
        radix.printAll(array);
   
    }
}

result:

Before sorting: 50 123 543 187 49 30 0 2 11 100	
After sorting: 0 2 11 30 49 50 100 123 187 543	


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895945&siteId=291194637