Java eight radix sorting algorithm of ordering

First, the action figure play

Second, the idea of ​​analysis

Each number i of the i-th digit radix sort Run row in the array to be placed tempj (j = 1-10) in the queue, then the data extracted from ten queue and place it back in the original array, until i is greater than the maximum number of digits to be rows.

1. The maximum number of bits in the array n bits, n times it is needed to be placed, for example, the maximum number of the array is 3 digits, the required discharge 3 times.

2. If the total number of m array, then an array of length m require ten of tempj (j = 0-9) for temporarily storing the number of i bits of the number j, e.g., Run 1, each digit is 0 temp0 will be assigned to the array, you will be number 1 is assigned to temp1 array ......

3. After completion of distribution, and then sequentially fetches data from the array tempj, follow the principle of advanced art, for example, an array {} 1,11,2,44,4, allocated for Run 1, temp1 = {1,11}, after temp2 = {2}, temp4 = {44,4}, {1,11,2,44,4} elements taken sequentially, first pass end

4. n times after the end of the cycle, the sort is complete

The idea of ​​the analysis, each pass execution flow as shown below:

By radix sort the array {53, 3, 542, 748, 14, 214, 154, 63, 616}:

Third, the negative heteroaryl analysis

1. The time complexity:

Every barrel assign keywords are required complexity of O (n) time, and get new keyword sequence after allocation they need time complexity of O (n) a.

If the data row can be divided into d keyword, the radix sorting time complexity will be O (d * 2n), d of course much smaller than n, so basically linear level.

Factor 2 may be omitted, and regardless of whether the ordered array, the maximum number of bits required bit discharged, it is always time complexity O (d * n). Wherein, n being the length of the array, d is the maximum number of bits.

2. Space complexity:

Radix sort spatial complexity is O (n + k), where k is the number of buckets, the need to assign the number n.

import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{10,6,3,8,33,27,66,9,7,88};
        radixSort(arr);
    }

    static void radixSort Private (int [] ARR) {
        // determine the maximum number of rows to be
        int maxLength = 0;
        for (int I = 0; I <arr.length; I ++) {
            IF (maxLength <ARR [I] )
                maxLength = ARR [I];
        }
        // find the maximum length of the maximum number
        maxLength = (maxLength + "") length ().;

        // array for temporarily storing data
        int [] [] temp = new new int [10] [arr.length];
        // number of records in each data array temp tub memory
        int [] counts = new int [10];
        // number of bits for each number i record
        int NUM = 0;
        // need to put an element for taking a position
        int index = 0;
        // length determines the maximum number of the sort
        for (int i = 0, n-=. 1; I <maxLength; I ++, n-* = 10) {
            for (int J = 0; J <arr.length; J ++) {
                NUM = ARR [J] / n-% 10;
                TEMP [NUM] [Counts [NUM]] = arr [J];
                Counts [NUM] ++;
            }
           
            // taken from the element back on the temp array arr
            for (int J = 0; J <counts.length; J ++) {
                for (int j2 = 0; j2 < counts [j]; j2 ++) {
                    arr[index] = temp[j][j2];
                    index++;
                }
                counts[j]=0;
            }
            index=0;
        }
        System.out.println(Arrays.toString(arr));
    }
}

 

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159801.htm