zoukankan      html  css  js  c++  java
  • 基数排序

    /**
     * 基数排序
     * 
     * @author lp
     *
     */
    public class RadixSort {
        
        public static void radixSort(int[] array) {
            // 首先确定排序的趟数;
            int max = array[0];
            for (int i = 1; i < array.length; i++) {
                if (array[i] > max) {
                    max = array[i];
                }
            }
            
            int time = 0;
            // 判断位数;
            while (max > 0) {
                max /= 10;
                time++;
            }
    
            // 建立10个队列;
            List<ArrayList> queue = new ArrayList<ArrayList>();
            for (int i = 0; i < 10; i++) {
                ArrayList<Integer> queue1 = new ArrayList<Integer>();
                queue.add(queue1);
            }
    
            // 进行time次分配和收集;
            for (int i = 0; i < time; i++) {
                // 分配数组元素;
                for (int j = 0; j < array.length; j++) {
                    // 得到数字的第time+1位数;
                    int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
                    ArrayList<Integer> queue2 = queue.get(x);
                    queue2.add(array[j]);
                    queue.set(x, queue2);
                }
                int count = 0;// 元素计数器;
                // 收集队列元素;
                for (int k = 0; k < 10; k++) {
                    while (queue.get(k).size() > 0) {
                        ArrayList<Integer> queue3 = queue.get(k);
                        array[count] = queue3.get(0);
                        queue3.remove(0);
                        count++;
                    }
                }
            }
    
        }
    
        public static void main(String[] args) {
            int array[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 101, 56, 17, 18, 23, 34, 15,35, 25, 53, 51 };
            
            radixSort(array);
            
            for (int element : array) {
                System.out.print(element + " ");
            }
        }
    
    }
  • 相关阅读:
    模板汇总 —— 杨式图表
    HDU 6634 网络流最小割模型 启发式合并
    网络流 从0开始学建图
    分层图 单调决策性DP
    模板汇总——笛卡尔树
    Bzoj 2127 happiness 最小割
    manacher --- 暂 旧版本
    Bzoj 3730 震波 动态点分治
    HDU
    Maven私服(Repository Manager)
  • 原文地址:https://www.cnblogs.com/rouqinglangzi/p/7521913.html
Copyright © 2011-2022 走看看