最后输出结果:[14,16,59,62,88]
public void radixSort(int[] nums){ //首先确定要排序的趟数 int max=nums[0]; for(int i=1;i<nums.length;i++){ if(nums[i]>max) max=nums[i]; } int time=0; while(max>0){ max/=10; time++; } //建10个桶 List<Queue<Integer>> bucket=new ArrayList<Queue<Integer>>(); for(int i=0;i<10;i++){ Queue<Integer> queue=new LinkedList<Integer>(); bucket.add(queue); } for(int i=0;i<time;i++){ for(int j=0;j<nums.length;j++){ int x=(int) ((nums[j]/Math.pow(10, i))); Queue<Integer> values=bucket.get(x); values.add(nums[j]); bucket.set(x, values); } //将桶里的数字顺序输出 int count=0; for(int k=0;k<10;k++){ while(!bucket.get(k).isEmpty()){ nums[count++]=bucket.get(k).poll(); } } } }