package week5;
import org.junit.Test;
public class RadixSort2 {
//测试函数
@Test
public void test(){
int[] A = {7467,1247,3275,6792,9187,9134,4675,1239};
radixsort(A,4);
}
/*基数排序
* @para A要排序的数
* @para A中数的长度
* */
public void radixsort(int[] A,int length){
int count = A.length;
int[] wei = new int[count]; //用来存放A中每个数的位
int div = 10;
for(int i=0; i<length; i++){
for(int j=0; j<count; j++){//获取每个数的个.十.百.千位
wei[j] = A[j]%div/(div/10);
System.out.print(wei[j]+" ");
}
System.out.println();
//使用选择排序将选中的每个数位进行排序
for(int m=0;m<count;m++){
int mix = wei[m]; //指定位的最小值
for(int n=m+1; n<count; n++){
if(wei[n]<mix){
mix = wei[n];
int temp = A[n];
for(int t=n; t>m; t--){ //将最小值抽取出来,然后数组中的其他数依次后退
A[t] = A[t-1];
wei[t] = wei[t-1];
}
A[m] = temp;
wei[m] = mix;
}
}
for(int x=0;x<count;x++){
System.out.print(A[x]+" ");
}
System.out.println("..........................");
}
div *= 10;
}
}
}
import org.junit.Test;
public class RadixSort2 {
//测试函数
@Test
public void test(){
int[] A = {7467,1247,3275,6792,9187,9134,4675,1239};
radixsort(A,4);
}
/*基数排序
* @para A要排序的数
* @para A中数的长度
* */
public void radixsort(int[] A,int length){
int count = A.length;
int[] wei = new int[count]; //用来存放A中每个数的位
int div = 10;
for(int i=0; i<length; i++){
for(int j=0; j<count; j++){//获取每个数的个.十.百.千位
wei[j] = A[j]%div/(div/10);
System.out.print(wei[j]+" ");
}
System.out.println();
//使用选择排序将选中的每个数位进行排序
for(int m=0;m<count;m++){
int mix = wei[m]; //指定位的最小值
for(int n=m+1; n<count; n++){
if(wei[n]<mix){
mix = wei[n];
int temp = A[n];
for(int t=n; t>m; t--){ //将最小值抽取出来,然后数组中的其他数依次后退
A[t] = A[t-1];
wei[t] = wei[t-1];
}
A[m] = temp;
wei[m] = mix;
}
}
for(int x=0;x<count;x++){
System.out.print(A[x]+" ");
}
System.out.println("..........................");
}
div *= 10;
}
}
}