【Eight sorts】--radix sorting (motion picture demonstration)

Introduction to Radix Sort (Bucket Sort)

(1) Radix sort (radix sort) belongs to "distribution sort", also known as "bucket sort" or bin sort. The elements are allocated to some "buckets" to achieve the sorting effect.

(2) The radix sorting method is a stable sorting method, and the radix sorting method is a stable sorting method with high efficiency.

(3) Radix Sort (Radix Sort) is an extension of bucket sort.

(4) Cardinality sorting was invented by Herman Holler in 1887. It is implemented like this: the integer is cut into different numbers according to the number of digits, and then compared according to each digit.
 

 step

1. Find the largest number among the elements to be sorted and determine its number of digits .

2. Prepare 10 arrays, 0-9 correspond to digits 0-9 respectively.

3. Put the unit digits of all the elements to be sorted into the corresponding 0-9 arrays. Then take out the subscript 0 from the array of 0-9 and put them into the original array to be sorted.

4. Put the tens digits of all the elements to be sorted into the corresponding 0-9 arrays. Then take them out from the array of 0-9 and put them into the original array to be sorted.

5. Put the hundreds digits of all elements to be sorted into the corresponding 0-9 arrays. Then take them out from the array of 0-9 and put them into the original array to be sorted.

......

6. After the above operations are performed until the largest bit, the sorting is complete .

Animation presentation

img

 algorithm code

public static void basicSort(int[] elem) {
        //1. 找最大值
        if(elem.length == 0) return;
        int max = elem[0];
        for (int i = 0; i < elem.length; i++) {
            if(max < elem[i]) {
                max = elem[i];
            }
        }

        // 2.确定最大值的位数
        int num = 0;
        while(max > 0) {
            max/=10;
            num++;
        }


        //3. 创建10个数组

        int[][] array = new int[10][elem.length];
        int x = 0;
        while(num > 0) {
            int i0=0,i1=0,i2=0,i3=0,i4=0,i5=0,i6=0,i7=0,i8=0,i9=0;

            //放入
            for (int i = 0; i < elem.length; i++) {
                int tmp = (elem[i]/(int)Math.pow(10,x)) % 10;

                switch(tmp) {
                    case 0: array[0][i0] = elem[i];i0++; break;
                    case 1: array[1][i1] = elem[i];i1++; break;
                    case 2: array[2][i2] = elem[i];i2++; break;
                    case 3: array[3][i3] = elem[i];i3++; break;
                    case 4: array[4][i4] = elem[i];i4++; break;
                    case 5: array[5][i5] = elem[i];i5++; break;
                    case 6: array[6][i6] = elem[i];i6++; break;
                    case 7: array[7][i7] = elem[i];i7++; break;
                    case 8: array[8][i8] = elem[i];i8++; break;
                    case 9: array[9][i9] = elem[i];i9++; break;
                }
            }


            // 2.取出排序
            int j = 0;

            int tmp0 = i0;
            while(i0 >0) {
                elem[j] = array[0][tmp0-i0];
                i0--;
                j++;
            }
            int tmp1 = i1;
            while(i1 >0) {

                elem[j] = array[1][tmp1-i1];
                i1--;
                j++;
            }
            int tmp2 = i2;
            while(i2 >0) {
                elem[j] = array[2][tmp2-i2];
                i2--;
                j++;
            }
            int tmp3 = i3;
            while(i3 >0) {

                elem[j] = array[3][tmp3-i3];
                i3--;
                j++;
            }
            int tmp4 = i4;
            while(i4 >0) {

                elem[j] = array[4][tmp4-i4];
                i4--;
                j++;
            }
            int tmp5 = i5;
            while(i5 >0) {

                elem[j] = array[5][tmp5-i5];
                i5--;
                j++;
            }
            int tmp6 = i6;
            while(i6 >0) {

                elem[j] = array[6][tmp6-i6];
                i6--;
                j++;
            }
            int tmp7 = i7;
            while(i7 >0) {

                elem[j] = array[7][tmp7-i7];
                i7--;
                j++;
            }
            int tmp8 = i8;
            while(i8 >0) {

                elem[j] = array[8][tmp8-i8];
                i8--;
                j++;
            }
            int tmp9 = i9;
            while(i9 >0) {

                elem[j] = array[9][tmp9-i9];
                i9--;
                j++;
            }


            x++;
            num--;

        }

    }

test print

public static void main(String[] args) {

        int arr[] = {45,98,36,85,20,99,79,96,12,13,52,66,88,10,30};
        basicSort(arr);
        for (int i: arr) {
            System.out.print(i+" ");
        }
    }

Radix sort description

(1) Radix sorting is an extension of traditional bucket sorting, which is very fast;

(2) Cardinality sorting is a classic way of exchanging space for time, which takes up a lot of memory. When sorting massive data, it is easy to cause OutOfMemoryError;

(3) Radix sorting is stable. [Note: Assume that there are multiple records with the same keyword in the sequence of records to be sorted. If sorted, the relative order of these records remains unchanged, that is, in the original sequence, r[i]=r[j] , and r[i] is before r[j], and in the sorted sequence, r[i] is still before r[j], the sorting algorithm is said to be stable; otherwise it is called unstable] ;

(4) For arrays with negative numbers, we do not use radix sorting for sorting, 
 

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/132305120