java to achieve common sorting algorithm (four) radix sort

Hello everyone, I am a duck:    

   Today share basis radix sort algorithm of ordering.

 

1. Radix Sort:


Principle: radix sort (radix sort) are "assigned ordering" (distribution sort), also known as "bucket Act" (bucket sort) or bin sort.

All values ​​to be compared (a positive integer) for the same number of uniform bit length, the shorter number of digits with leading zeros. Then, from the lowest Start, once sorted. This has been sorted from the lowest to the highest order of the bits is completed, the series becomes an ordered sequence.

Ideas:

Radix sort, namely a digital one bit to sort, an idea algorithm we often use ordinary life: If you want to date sorting, date by year, month, day consisting of, for that matter, we often use it is the first year of the comparison, if the same, compare the month, if still relatively the same day.

The following four numbers sort: 123,312,245,531

   Bits => ten => one hundred

   531      312      123

   312      123      245

   123      531      312

   245      245      531

Code:

 /**
     * 基数排序
     * @param d
     * @param array
     *  时间复杂度 O的log2 N
     *  基数排序 运用二维数组来分别比较每一位,个位、十位、百位…
     * 输入10个整数的数组
     */
    private void radixSort(int d,int[] array){
        long nowTime = System.nanoTime();
        int n=1;//代表位数对应的数:1,10,100000...
        int k=0;//保存每一位排序后的结果用于下一位的排序输入
        int[][] bucket=new int[10][array.length];//排序桶用于保存每次排序后的结果,这一位上排序结果相同的数字放在同一个桶里
        int[] num=new int[array.length];//用于保存每个桶里有多少个数字 ,最多为输入数组长度
        while(n<=d)
        {
            for(int e:array) //将数组array里的每个数字放在相应的桶里
            {
                int digit=(e/n)%10;
                bucket[digit][num[digit]]=e;
                num[digit]++;
            }
            for(int i=0;i<array.length;i++)//将前一个循环生成的桶里的数据覆盖到原数组中用于保存这一位的排序结果
            {
                if(num[i]!=0)//这个桶里有数据,从上到下遍历这个桶并将数据保存到原数组中
                {
                    for(int j=0;j<num[i];j++)
                    {
                        array[k]=bucket[i][j];
                        k++;
                    }
                }
                num[i]=0;//将桶里计数器置0,用于下一次位排序
            }
            n*=10;
            k=0;//将k置0,用于下一轮保存位排序结果
        }
        System.out.println("基数排序,花费时间(ms):" + ((System.nanoTime() - nowTime) / 1000000.0) + "ms");
    }

Radix sort algorithm is stable, very efficient, which complexity is O (nlog (r) m), where r is the radix taken, and m is the number of heaps. But it can only be in the sort of integers, and the need to use a supplementary space.

The above limitation is that some of the code applied to the selected group value:

int [] x = {25 ,11 ,22 ,34 ,15 ,44 ,76, 66, 100, 8 ,14, 20 ,2, 5 ,1 };

For example, this array, the base should be selected 3. Attach a method of selecting the base value.

 /**
     * 获取基数排序中的基数
     * @param array
     * @return
     */
    public int getRadixBasicNumber(int[] array){
        if(array == null && array.length ==0) {
            return 0;
        }
        int max =0;
        //1获取最大的绝对值的数
        for(int i =0;i<array.length;i++) {
            if(Math.abs(max)<Math.abs(array[i])) {
                max = array[i];
            }
        }
        int times = 0;
        if(max<0) max = -max;
        //2求出最大的绝对值的数,是10的times次幂。
        while(max >0) {
            max = max/10;
            times ++;
        }
        return times;
    }

 

Time-consuming comparison:

FIG 10W pieces of random data run:

Respectively, compared radix sort, in-line sort, shell sort, and quick sort. Gap is obvious.

FIG 50W pieces of random data run:

Respectively, compared radix sort, in-line sort, shell sort, and quick sort. Gap is obvious.

FIG 100W pieces of random data run:

Respectively, compared radix sort, in-line sort, shell sort, and quick sort. Gap is obvious.

to sum up:

Radix Sort

Advantages: high efficiency.

Disadvantages: large memory footprint, only sort of positive integers (a two-dimensional array do barrels)

The worst time complexity: O (P (N + B)).

The average time complexity is: O (P (N + B)).

Where P is the number of times of sorting, N being the number of elements to be sorted, B is the number of barrels.

Compare various sorting methods:

 

More sorting algorithm:

Bubble sort:   https://blog.csdn.net/Angry_Mills/article/details/81057900

Insertion sort:   https://blog.csdn.net/Angry_Mills/article/details/81208700

Quick Sort:   https://blog.csdn.net/Angry_Mills/article/details/83339390

Published 115 original articles · won praise 58 · Views 230,000 +

Guess you like

Origin blog.csdn.net/Angry_Mills/article/details/83383572