Java-Bucket Sort (Count Sort & Radix Sort)

Bucket sorting is not based on comparison sorting

Counting sort (non-negative decimal): first prepare ten queues and then enter the bucket according to the single digit

Bucket sorting is an idea: one pit has several radishes

The idea of ​​counting and sorting is very simple: the age of the employee is (18-35), then create (35-18+1) buckets, then traverse the array of employee ages and put them in the corresponding buckets and then output it.

    public static void radixSort(int [] arr) {
        if(arr==null||arr.length<2) {
            return;
        }
        radixSort(arr,0,arr.length-1,maxbits(arr));
    }
    public static int maxbits(int [] arr) {
        int max = Integer.MIN_VALUE;
        for(int i = 0;i<arr.length;i++) {
            max = Math.max(max,arr[i]);
        }
        int count = 0;
        while(max!=0) {
            max/=10;
            count++;
        }
        return count;
    }
    public static int getbits(int num,int bitnum) {
        for(int i = 1;i<=bitnum-1;i++) {
            num = num/10;
        }
        return num%10;
    }
    public static void radixSort(int [] arr,int L,int R,int digit) {
        int radix = 10;
        int [] help = new int [R-L+1];
        int i = 0;
        int j = 0;
        for(int d = 1;d<=digit;d++) {
            int [] count = new int [radix];
            for(i = L; i <= R; i++) {
                j = getbits(arr[i], d);
                count[j]++;
            }
            for(i = 1;i<radix;i++) {
                count[i] += count[i-1];
            }
            for(i = R;i>=L;i--) {
                j = getbits(arr[i], d); 
                help[count[j]-1] = arr[i];
                count[j]--;
            }
            for (i = L, j = 0; i <= R; i++, j++) {
                arr[i] = help[j];
            }
        }
        
    }
    public static void main(String[] args) {
       int [] arr = {1,3,5,7,9,2,4,6,8,10};
       radixSort(arr);
       System.out.println(Arrays.toString(arr));
    }

The principle of radix sorting is not complicated. There are a batch of numbers, first sort by the size of the single digit, and then the ten digits. …

For example, the number {11,1,18,4,27,635,621,55,9}

First ranked first: 11,1,621,4,635,55,27,18,9

Then use the second position on this basis: 1,4,9,11,18,621,27,635,55

On this basis, use the third row: 1,4,9,11,18,27,55,621,635

What's the matter with this principle? First use the sorting on the small digits, but the numbers on the high digits have a greater impact on the size of the number, so then sort them on this basis

The code implementation looks complicated in order to reduce space consumption....

The part that the male ♂ understands is actually only the part from line 41 to line 48. One is accumulated and the other is placed in the code comment in reverse order. The (also difficult to understand) explanation has been given

public static void radixSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        radixSort(arr, 0, arr.length - 1, maxbits(arr));
    }

    public static int maxbits(int[] arr) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max, arr[i]);
        }
        int res = 0;
        while (max != 0) {
            res++;
            max /= 10;
        }
        return res;
    }

    // arr[l..r]排序  ,  digit
    // l..r    3 56 17 100    3
    public static void radixSort(int[] arr, int L, int R, int digit) {
        final int radix = 10;
        int i = 0, j = 0;
        // 有多少个数准备多少个辅助空间
        int[] help = new int[R - L + 1];
        for (int d = 1; d <= digit; d++) { // 有多少位就进出几次
            // 10个空间
            // count[0] 当前位(d位)是0的数字有多少个
            // count[1] 当前位(d位)是(0和1)的数字有多少个
            // count[2] 当前位(d位)是(0、1和2)的数字有多少个
            // count[i] 当前位(d位)是(0~i)的数字有多少个
            int[] count = new int[radix]; // count[0..9]
            for (i = L; i <= R; i++) {
                // 103  1   3
                // 209  1   9
                j = getDigit(arr[i], d);
                count[j]++;
            }//计算d位是j的数有多少个 计数排序啊
            for (i = 1; i < radix; i++) {
                count[i] = count[i] + count[i - 1];//累加上去 当前位是i~0的有多少个
            }//意义就是下面的arr[i]它该放在第几个
            for (i = R; i >= L; i--) {//倒序放置 是在上一次(前一位)排序基础上再进行排序 算是计数排序的核心
                j = getDigit(arr[i], d);
                help[count[j] - 1] = arr[i];//
                count[j]--;
            }
            for (i = L, j = 0; i <= R; i++, j++) {
                arr[i] = help[j];//把help中的值赋给arr
            }
        }
        
        
        
        
    }

    public static int getDigit(int x, int d) {
        return ((x / ((int) Math.pow(10, d - 1))) % 10);
    }

Guess you like

Origin blog.csdn.net/chara9885/article/details/129756289