非比较类排序算法:创建10个桶,分别用于存放个位/十位...为0-9的元素,通过不停的对数组遍历,让元素从右到左(从个位开始)依次放入桶中,并对每一个桶里面的元素进行排序,得到每个元素之间的相对位置,并取出再重复上面的操作。直到将最长(最大的数的)位数遍历完,就完成了排序。
import java.util.*;
public class Main9 {
//基数排序
public static void main(String[] args) {
long l = System.currentTimeMillis();
String s="9";
System.out.println((int)s.charAt(0));
int aa[]=new int[]{-1,-4,-8,12,-99,23,25,4,5,2,1,3};
int a[] = new int[]{54,45,13,43,56,10,1,444,3,41,5, 2, 3, 1, 8, 99,88,77,881,784,23,44,44,112,55,23,56,7,877,356,321,54,12,533,6,2,6,1, 9,12,5,112,5,25,6,72,6,7,81,5,6,31,3,4,1,2,3,5,64,122,344,11,24,5,63,34,5,631,3,455,3,12,6,67,77,2,2,33,4,55};
int b[] = new int[]{-1100,-2200,-4,0,-8,99,-993,994,-98,4412,133,455,613,743,7122,56,7544,1223,566,112,442,11224,56412,56,4678,12234,673312,77,413,123,435,23,555,64,61,122,45,1,4,67,113,64,774,34,31,67,237,137,35,73,73,13,67,89,59,90,3,83,3,1,313,199,883,89,1,33,45,67,133,664,73,22,14,-74, 48, -20, 2, 10, -84, -5, -9, 11, -24, -91, 2, -71, 64, 63, 80, 28, -30, -58, -11, -44, -87, -22, 54, -74, -10, -55, -28, -46, 29, 10, 50, -72, 34, 26, 25, 8, 51, 13, 30, 35, -8, 50, 65, -6, 16, -2, 21, -78, 35, -13, 14, 23, -3, 26, -90, 86, 25, -56, 91, -13, 92, -25, 37, 57, -20, -69, 98, 95, 45, 47, 29, 86, -28, 73, -44, -46, 65, -84, -96, -24, -12, 72, -68, 93, 57, 92, 52, -45, -2, 85, -63, 56, 55, 12, -85, 77, -39};
System.out.println("===="+b.length);
bucket(b);
for (int j=0;j< b.length;j++)
System.out.println(b[j]);
long l1 = System.currentTimeMillis();
System.out.println("=="+(l1-l));
}
public static void bucket(int a[]){
int len=a.length;
if (len<=1)
return ;
int i=0;
int max=Integer.MIN_VALUE;
while (i<len){
max=Math.max(max,a[i++]);
}
int nums=(max>0?(max+"").length():((max*(-1))+"").length());
int count=10;
ArrayList<ArrayList<Integer>> bucket=new ArrayList<>();
for (int j=0;j<count;j++)
bucket.add(new ArrayList<>());
for(i=1;i<=nums;i++){
for(int j=0;j<len;j++){
String s = (a[j]>=0?a[j]:a[j]*(-1)) + "";
if(s.length() - i>=0&&a[i]>=0){
int c = s.charAt(s.length() - i)-48;
bucket.get(c).add(a[j]);
} else if(s.length()-i<0){
bucket.get(0).add(a[j]);
}
}
int j=0;
int m=0;
while (j<len&&m<bucket.size()){
bucket.get(m).sort(Comparator.comparingInt(Integer::valueOf));
int size = bucket.get(m).size();
for(int k=0;k<size;k++)
a[j++]=bucket.get(m).get(k);
bucket.get(m++).clear();
}
}
}
}