码迷,mamicode.com
首页 > 编程语言 > 详细

基数排序

时间:2017-12-30 22:36:54      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:temp   body   system   style   post   length   开始   --   []   

 

    public static int[] radixSort(int[] arr, int radix, int d){
        //用于暂存元素
        int[] temp = new int[arr.length];
        //用于计数排序
        int[] count = new int[radix];
        int divide = 1;
        
        for(int i=0;i<d;i++){
            System.arraycopy(arr, 0, temp, 0, arr.length);
            // 重置count数组,开始统计下一个关键字  
            Arrays.fill(count, 0);
            
            // 计算每个待排序数据的子关键字  
            for(int j=0;j<arr.length;j++){
                int tempKey = (temp[j]/divide)%radix;
                count[tempKey]++;
            }
            
            for(int j=1;j<radix;j++){
                count[j] = count[j] + count[j-1];
            }
            
            // 按子关键字对指定的数据进行排序  
            for(int j=arr.length-1;j>=0;j--){
                int tempKey = (temp[j]/divide)%radix;
                count[tempKey]--;
                arr[count[tempKey]] = temp[j];
            }
            
            divide = divide * radix;
        }
        return arr;
    }

 

基数排序

标签:temp   body   system   style   post   length   开始   --   []   

原文地址:https://www.cnblogs.com/weishao-lsv/p/8151286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!