大家好,我需要为任意数量的数字和值编写 RADIX 排序算法。我认为使用 LL 来操作比使用 Arrays 来存储 0-9 值更容易。我认为使用递归来解决这个问题会更好,但是我遇到了困难并且出错了
Exception in thread "main" java.lang.StackOverflowError
at java.util.LinkedList$Node.<init>(Unknown Source)
at java.util.LinkedList.linkLast(Unknown Source)
at java.util.LinkedList.add(Unknown Source)
我真的很感激任何指导!:)
import java.util.LinkedList;
import java.util.Scanner;
public class pt2 {
long[] mine;
int digits;
Scanner sn = new Scanner(System.in);
public pt2() {
makeArray();
add();
RADIX(mine,1);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
void RADIX(int[] kiki, int digi){
while(digi<=digits){
LinkedList<Integer> l0 = new LinkedList<Integer>();
LinkedList<Integer> l1 = new LinkedList<Integer>();
LinkedList<Integer> l2 = new LinkedList<Integer>();
LinkedList<Integer> l3 = new LinkedList<Integer>();
LinkedList<Integer> l4 = new LinkedList<Integer>();
LinkedList<Integer> l5 = new LinkedList<Integer>();
LinkedList<Integer> l6 = new LinkedList<Integer>();
LinkedList<Integer> l7 = new LinkedList<Integer>();
LinkedList<Integer> l8 = new LinkedList<Integer>();
LinkedList<Integer> l9 = new LinkedList<Integer>();
LinkedList[] cap ={l0,l1,l2,l3,l4,l5,l6,l7,l8,l9};
for( int j=0;j<kiki.length;j++){
int k=0;
while(k<10){
//need to put the number in the correct LL but how
int jk = digital(kiki[j],(int)(Math.pow(10,digi)));
if(jk==k){
cap[k].add(kiki[j]);
k=11;
}
k++;
}
}
int i = 0;
while(i<kiki.length){
int p=0;
while(p<10){
while(cap[p].isEmpty()==false){
kiki[i]=(int) cap[p].removeFirst();
}
p++;
}
i++;
}
digi++;
}
}
public int digital(int num, int pos){
int i = (int) (num % Math.pow(10,pos));
i= (int)(i/Math.pow(10, pos-1));
return i;
}
void add(){
int i=0;
while(i<mine.length){
mine[i] = (long)(Math.random()*digits);
i++;
}
}
void makeArray(){
System.out.println("Max size?");
mine = new long[sn.nextInt()];
System.out.println("Max digits?);
if(digits<0){
System.out.println("bad USer");
}else{
digits = sn.nextInt();
}
}
}
现在这回来了
最大尺寸?最多 10 个数字?2 未排序
74 63 87 52 1 99 10 74 67 49
已排序 49 63 87 52 1 99 10 74 67 49 结束