基数排序
将数组中的每个数分别按个位、十位、百位…进行排序。
获取数中的每一位
public static void main(String[] args){
int number = 23456;
int bit;
System.out.print("该数的个位:");
bit = number % 10;
System.out.println(bit);
System.out.print("该数的十位:");
bit = number / 10 % 10;
System.out.println(bit);
System.out.print("该数的百位:");
bit = number / 100 % 10;
System.out.println(bit);
System.out.print("该数的千位:");
bit = number / 1000 % 10;
System.out.println(bit);
System.out.print("该数的万位:");
bit = number / 10000 % 10;
System.out.println(bit);
}
该数的个位:6
该数的十位:5
该数的百位:4
该数的千位:3
该数的万位:2
基本实现
public class RadixSort {
public int[] sort(int[] array){
if(array.length == 0)
return null;
int maxBit = 1;
int number = 10;
for(int i = 0;i < array.length;i ++){
if(array[i] >= number){
number = number * 10;
maxBit ++;
}
}
System.out.println("最大值的位数:" + maxBit);
return sort(maxBit,array);
}
private int[] sort(int maxBit,int[] array) {
int[][] arrays = new int[10][array.length];
int[] numberes;
numberes = new int[10];
int bit;
for (int i = 0; i < array.length; i++) {
bit = array[i] % 10;
arrays[bit][numberes[bit]] = array[i];
numberes[bit]++;
}
int index = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < numberes[i]; j++) {
array[index] = arrays[i][j];
index++;
}
}
System.out.println("第一次排序:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
numberes = new int[10];
int s_bit;
for (int i = 0; i < array.length; i++) {
s_bit = (array[i] / 10) % 10;
arrays[s_bit][numberes[s_bit]] = array[i];
numberes[s_bit]++;
}
int s_index = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < numberes[i]; j++) {
array[s_index] = arrays[i][j];
s_index++;
}
}
System.out.println("第二次排序:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
numberes = new int[10];
int t_bit;
for (int i = 0; i < array.length; i++) {
t_bit = (array[i] / 100) % 10;
arrays[t_bit][numberes[t_bit]] = array[i];
numberes[t_bit]++;
}
int t_index = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < numberes[i]; j++) {
array[t_index] = arrays[i][j];
t_index++;
}
}
System.out.println("第三次排序:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
return array;
}
}
测试类
public class Demo {
public static void main(String[] args) {
int[] array = new int[]{10,2,9,121,28,56,88,137};
RadixSort sort = new RadixSort();
array = sort.sort(array);
System.out.println();
System.out.println("排好序的数组:");
for(int i : array){
System.out.print(i + " ");
}
}
}
结果
最大值的位数:3
第一次排序:
10 121 2 56 137 28 88 9
第二次排序:
2 9 10 121 28 137 56 88
第三次排序:
2 9 10 28 56 88 121 137
排好序的数组:
2 9 10 28 56 88 121 137
优化
public class RadixSort {
public int[] sort(int[] array){
if(array.length == 0)
return null;
int maxBit = 1;
int number = 10;
for(int i = 0;i < array.length;i ++){
if(array[i] >= number){
number = number * 10;
maxBit ++;
}
}
System.out.println("最大值的位数:" + maxBit);
return sort(maxBit,array);
}
private int[] sort(int maxBit,int[] array){
int[][] arrays = new int[10][array.length];
int[] numberes;
int bit;
int index;
int flag = 0;
int num = 1;
while(flag < maxBit){
if(flag == 0)
num = 1;
else
num = num * 10;
numberes = new int[10];
for(int i = 0;i < array.length;i ++){
bit = (array[i] / num) % 10;
arrays[bit][numberes[bit]] = array[i];
numberes[bit] ++;
}
index = 0;
for(int i = 0;i < 10;i ++){
for(int j = 0;j < numberes[i];j ++){
array[index] = arrays[i][j];
index ++;
}
}
System.out.println("第"+ flag + "次排序:");
for(int i = 0;i < array.length;i ++){
System.out.print(array[i] + " ");
}
System.out.println();
flag ++;
}
return array;
}
}
测试类
public class Demo {
public static void main(String[] args) {
int[] array = new int[]{99,4,0,123,567,890,1439,1374};
RadixSort sort = new RadixSort();
array = sort.sort(array);
System.out.println("排好序的数组:");
for(int i : array){
System.out.print(i + " ");
}
}
}
结果
最大值的位数:4
第0次排序:
0 890 123 4 1374 567 99 1439
第1次排序:
0 4 123 1439 567 1374 890 99
第2次排序:
0 4 99 123 1374 1439 567 890
第3次排序:
0 4 99 123 567 890 1374 1439
排好序的数组:
0 4 99 123 567 890 1374 1439
|