基数排序

来源:互联网 发布:java springmvc 分页 编辑:程序博客网 时间:2024/06/05 00:26
对各个位置上的数,按分别按个位排序,倒入桶中,然后倒出后,按十位排序。。。。
public class RadixSort {    public int[] radixSort(int[] A, int n) {        // write code here        List<List<Integer>> radixList = new ArrayList<>();        for (int i = 0; i < 10; i++) {            radixList.add(i, new ArrayList<Integer>());        }        for(int i = 1; i<=4;i++){  //数字为0~10000 此i为从个位起的第i位数            for(int j =0; j< n ;j++){                int tmp =(int) (A[j]/(Math.pow(10,i-1))) %10;  //第i位的 数值                radixList.get(tmp).add(A[j]);            }            int index = 0;            for (int k = 0; k < 10; k++) {                while(radixList.get(k).size()!=0){                    A[index++] = radixList.get(k).get(0);                    radixList.get(k).remove(0);                }            }        }        return A;    }}

0 0
原创粉丝点击