0
点赞
收藏
分享

微信扫一扫

基数排序(这里假设数据的最高位为3)

基本思想:在需要排序的一串数据中,取最长位数为参考,不足最长位数的数据要在前面补零,然后形成一串相同位数的数据,最后通过比较这串数据的个位数,十位数,百位数….最后就会得到一个有序的序列。 用Java实现如下所示:

import java.util.Arrays;

public class Test1 {
    public static void main(String[] args) {
        //创建一个数组
        int[] array = new int[]{53, 542, 3, 63, 14, 214, 154, 748, 616};
        System.out.print("原数据为:" + Arrays.toString(array));
        sort(array);
        System.out.print("\n通过基数排序后的数据为:" + Arrays.toString(array));

    }

    //基数排序的方法
    public static void sort(int[] array) {
        //获取最大位数
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        //把int类型转换为String类型 ,便于统计位数
        int maxLenth = (max + "").length();

        //定义二维数组,作用为桶,桶的大小为上面需要排序的一维数组的大小,数量为10个
        /**
         * 作用:首先求出数据的个位数,然后通过个位数来确定数组下标,通过个位数将数据放入对应的桶中
         *      然后取出数据,再通过十位数确定下标,将数据放入对应的桶中
         *      再次取出数据,再通过百位数确定下标,然后将数据放入对应的桶中,最后得到一个有序的序列
         *
         *      注意:这里需要一个辅助数组,这个数组里面的下标值为几号桶,对应的数据为桶中有几个有效的数据,利用辅助数组的特点可以利用来确定数据放入桶中时放入那一个索引
         *      因为如果桶中的前面有数据时,就不能直接放入其中,而需要接着放在桶中的后面,所以我们需要使用辅助数组来确定我们具体应该放在那一个索引位置中。
         *
         * */
        int[][] bucket = new int[10][array.length];
        //定义辅助数组
        int[] bucketElementCount = new int[10];
        //循环无序的序列
        for (int i = 0; i < array.length; i++) {
            //获取个位数
            int locationElement = array[i] % 10;
            //通过辅助数组确定将数据放入桶中的什么位置
            bucket[locationElement][bucketElementCount[locationElement]] = array[i];
            //修改辅助数组中的值
            bucketElementCount[locationElement]++;
        }

        //原数组的索引
        int index = 0;
        //通过辅助数组来遍历桶中的数据,并且将数据放入原数组中
        for (int i = 0; i < bucketElementCount.length; i++) {
            if (bucketElementCount[i] != 0) {
                //通过辅助数组的值来判断循环的次数
                for (int j = 0; j < bucketElementCount[i]; j++) {
                    //将桶中的数据赋值给原数组
                    array[index++] = bucket[i][j];
                    //将桶中的数据置零
                    bucket[i][j] = 0;
                }
                //将赋值数组置零
                bucketElementCount[i] = 0;
            }
        }
        //将原数组索引置零
        index = 0;

        //判断十位数
        for (int i = 0; i < array.length; i++) {
            //获取十位数
            int locationElement = array[i] / 10 % 10;
            //通过辅助数组确定将数据放入桶中的什么位置
            bucket[locationElement][bucketElementCount[locationElement]] = array[i];
            //修改辅助数组中的值
            bucketElementCount[locationElement]++;
        }
        //通过辅助数组来遍历桶中的数据,并且将数据放入原数组中
        for (int i = 0; i < bucketElementCount.length; i++) {
            if (bucketElementCount[i] != 0) {
                //通过辅助数组的值来判断循环的次数
                for (int j = 0; j < bucketElementCount[i]; j++) {
                    //将桶中的数据赋值给原数组
                    array[index++] = bucket[i][j];
                    //将桶中的数据置零
                    bucket[i][j] = 0;
                }
                //将赋值数组置零
                bucketElementCount[i] = 0;
            }
        }
        //将原数组索引置零
        index = 0;

        //判断百位数
        for (int i = 0; i < array.length; i++) {
            //获取个位数
            int locationElement = array[i] / 100;
            //通过辅助数组确定将数据放入桶中的什么位置
            bucket[locationElement][bucketElementCount[locationElement]] = array[i];
            //修改辅助数组中的值
            bucketElementCount[locationElement]++;
        }
        //通过辅助数组来遍历桶中的数据,并且将数据放入原数组中
        for (int i = 0; i < bucketElementCount.length; i++) {
            if (bucketElementCount[i] != 0) {
                //通过辅助数组的值来判断循环的次数
                for (int j = 0; j < bucketElementCount[i]; j++) {
                    //将桶中的数据赋值给原数组
                    array[index++] = bucket[i][j];
                    //将桶中的数据置零
                    bucket[i][j] = 0;
                }
                //将辅助数组置零
                bucketElementCount[i] = 0;
            }
        }


    }

}

输出结果如下所示:

原数据为:[53, 542, 3, 63, 14, 214, 154, 748, 616]
通过基数排序后的数据为:[3, 14, 53, 63, 154, 214, 542, 616, 748]
举报

相关推荐

0 条评论