Cardinality sorting (Java)

Posted by eagleweb on Sat, 27 Jun 2020 11:09:37 +0200

Cardinality sorting (Java)

Blog description

The information involved in this article comes from Internet collation and personal summary, which means personal learning and experience summary. If there is any infringement, please contact me to delete, thank you!

Introduction to cardinality sort (bucket sort)

radix sort belongs to distribution sort, also known as bucket sort or bin sort. As the name implies, it allocates elements to be sorted to some buckets by the value of each bit of key value to achieve sorting function

The cardinality sorting method belongs to the stable sorting, while the cardinality sorting method belongs to the stable sorting with high efficiency

Radix sort is an extension of bucket sort

The cardinal order was invented by Herman Horsley in 1887. It is realized by cutting integers into different numbers according to the number of digits, and then comparing them according to each digit.

Basic idea of cardinal ordering

All the values to be compared shall be unified into the same digit length, and zero shall be filled in front of the shorter digit. Then, start from the lowest order and sort one by one. In this way, from the lowest ranking to the completion of the highest ranking, the sequence becomes an ordered sequence.

characteristic

Space for time, stable

code

package cn.guizimo.sort;

import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int arr[] = {53,45,6,378,15,234,78};
        System.out.println("Before sorting");
        System.out.println(Arrays.toString(arr));
        radixSort(arr);
        System.out.println("After sorting");
        System.out.println(Arrays.toString(arr));
    }

    public static void radixSort(int arr[]) {
        //Get the maximum number of digits
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        //Calculation digit
        int maxLength = (max + "").length();

        int[][] bucket = new int[10][arr.length];
        int[] bucketElemtCounts = new int[10];

        for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
            for (int j = 0; j < arr.length; j++) {
                int digitOfElemt = arr[j] / n % 10;
                bucket[digitOfElemt][bucketElemtCounts[digitOfElemt]] = arr[j];
                bucketElemtCounts[digitOfElemt]++;
            }
            int index = 0;
            for (int k = 0; k < bucketElemtCounts.length; k++) {
                if (bucketElemtCounts[k] != 0) {
                    for (int l = 0; l < bucketElemtCounts[k]; l++) {
                        arr[index++] = bucket[k][l];
                    }
                }
                bucketElemtCounts[k] = 0;
            }
            System.out.println("The first"+(i+1)+"Round order");
            System.out.println(Arrays.toString(arr));
        }

    }
}

test

Test speed

package cn.guizimo.sort;

import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int max = 80000;
        int[] arr = new int[max];
        for (int i = 0; i < max; i++) {
            arr[i] = (int)(Math.random() * 80000);
        }
        long date1 = System.currentTimeMillis();
        radixSort(arr);
        long date2 = System.currentTimeMillis();
        System.out.println("Displacement Hill ordering"+max+"The time of the array is:"+(date2-date1));
    }

    public static void radixSort(int arr[]) {
        //Get the maximum number of digits
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        //Calculation digit
        int maxLength = (max + "").length();

        int[][] bucket = new int[10][arr.length];
        int[] bucketElemtCounts = new int[10];

        for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
            for (int j = 0; j < arr.length; j++) {
                int digitOfElemt = arr[j] / n % 10;
                bucket[digitOfElemt][bucketElemtCounts[digitOfElemt]] = arr[j];
                bucketElemtCounts[digitOfElemt]++;
            }
            int index = 0;
            for (int k = 0; k < bucketElemtCounts.length; k++) {
                if (bucketElemtCounts[k] != 0) {
                    for (int l = 0; l < bucketElemtCounts[k]; l++) {
                        arr[index++] = bucket[k][l];
                    }
                }
                bucketElemtCounts[k] = 0;
            }
        }
    }
}

thank

Shangsilicon Valley

Omnipotent network

And the industrious self

Topics: Programming Java network