基排序(适合数的间隔大,位数多)
用到了桶排序,0-9共计10个桶,从各位开始每位数比较
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String[] a = new String[10];
a[0] = "123456789";
a[1] = "123456781";
a[2] = "124456789";
a[3] = "163456789";
a[4] = "124453789";
a[5] = "523456789";
a[6] = "123456781";
a[7] = "123456789";
a[8] = "123456709";
a[9] = "123456799";
radixNumber(a, 9);
for (String s : a) {
System.out.println(s);
}
}
public static void radixNumber(String[] a, int length) {
ArrayList<String>[] buckets = new ArrayList[10];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList<>();
}
for (int i = length - 1; i >= 0; i--) {
for (String s : a) {
buckets[s.charAt(i) - '0'].add(s);
}
int k = 0;
for (ArrayList<String> bucket : buckets) {
for (String S : bucket) {
a[k] = S;
k++;
}
bucket.clear();
}
}
}
}
public class Main {
public static void main(String[] args) {
String[] a = new String[10];
a[0] = "123456789";
a[1] = "123456781";
a[2] = "124456789";
a[3] = "163456789";
a[4] = "124453789";
a[5] = "523456789";
a[6] = "123456781";
a[7] = "123456789";
a[8] = "123456709";
a[9] = "123456799";
radixNumber(a, 9);
for (String s : a) {
System.out.println(s);
}
}
public static void radixNumber(String[] a, int length) {
ArrayList<String>[] buckets = new ArrayList[10];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList<>();
}
for (int i = length - 1; i >= 0; i--) {
for (String s : a) {
buckets[s.charAt(i) - '0'].add(s);
}
int k = 0;
for (ArrayList<String> bucket : buckets) {
for (String S : bucket) {
a[k] = S;
k++;
}
bucket.clear();
}
}
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17873833.html
千万千万赵千万