Need advice on a Radix Sort problem...

Alright I have pretty much completed my algorithim. What I am supposed to do is take several 3-digit (ex. 203, 331, etc.) from an in.dat file. I have taken the in file input and put the integers into an ArrayList. I have then passed the ArrayList to my method where I begin to do Radix Sort...

Radix Sort (in case you don't know):

Take the numbers 122, 221, and 120...

First step: checking the last number (index = 3) and sorting them into seperate queues.

qZero = [120] , qOne = [221] , qTwo = [122]

Then poll() those off and add to back to the ArrayList...

Some of my problems:

- not sure how to pinpoint at the index on the outside and move in. I tried type casting and using charAt()... not sure if that is a good way of thinking of it.

- Not sure if my algorithim as a whole is correct... should I be using that many if-else statements, while loops, AND the recursion?

Here is my code thus far... I don't want the answer, but I do need advice!! Thanks a ton in advance:)

import java.util.*;

import java.io.*;

publicclass RadixSort

{

publicstaticvoid main(String[] args)throws IOException

{

ArrayList<Integer> aList =new ArrayList<Integer>();

File file =new File("in.dat");

PrintWriter outFile =new PrintWriter(new File("out.dat"));

Scanner fileIn =new Scanner(file);

while (fileIn.hasNextInt())

{

int a = fileIn.nextInt();

aList.add(a);

}

System.out.println(aList);

System.out.println(rOrder(aList, 3));

}

publicstatic ArrayList rOrder(ArrayList<Integer> aList,int n)

{

ArrayList<Integer> arr =new ArrayList<Integer>();

Queue<String> qZero =new LinkedList<String>();

Queue<String> qOne =new LinkedList<String>();

Queue<String> qTwo =new LinkedList<String>();

Queue<String> qThree =new LinkedList<String>();

Queue<String> qFour =new LinkedList<String>();

Queue<String> qFive =new LinkedList<String>();

Queue<String> qSix =new LinkedList<String>();

Queue<String> qSeven =new LinkedList<String>();

Queue<String> qEight =new LinkedList<String>();

Queue<String> qNine =new LinkedList<String>();

String data ="";

while (!aList.isEmpty())

{

data = (String) aList.remove(n);

if (data.charAt(n) =='0')

qZero.offer(data);

elseif (data.charAt(n) =='1')

qOne.offer(data);

elseif (data.charAt(n) =='2')

qTwo.offer(data);

elseif (data.charAt(n) =='3')

qThree.offer(data);

elseif (data.charAt(n) =='4')

qFour.offer(data);

elseif (data.charAt(n) =='5')

qFive.offer(data);

elseif (data.charAt(n) =='6')

qSix.offer(data);

elseif (data.charAt(n) =='7')

qSeven.offer(data);

elseif (data.charAt(n) =='8')

qEight.offer(data);

else

qNine.offer(data);

data ="";

}

while(!qZero.isEmpty())

arr.add((int)qZero.poll());

while(!qOne.isEmpty())

arr.add((int)qOne.poll());

while(!qTwo.isEmpty())

arr.add((int)qTwo.poll());

while(!qThree.isEmpty())

arr.add((int)qThree.poll());

while(!qFour.isEmpty())

arr.add((int)qFour.poll());

while(!qFive.isEmpty())

arr.add((int)qFive.poll());

while(!qSix.isEmpty())

arr.add((int)qSix.poll());

while(!qSeven.isEmpty())

arr.add((int)qSeven.poll());

while(!qEight.isEmpty())

arr.add((int)qEight.poll());

while(!qNine.isEmpty())

arr.add((int)qNine.poll());

if (n < 0)

return arr;

else

rOrder(arr, n-1);

}

}

[6872 byte] By [Nerd_I_Bea] at [2007-10-2 6:36:53]
# 1

Also sorry on the actual sorting the next step would be

qZero = [ ] , qOne = [ ] , qTwo = [ 120, 221, 122]

put back into the ArrayList..

Then the next step being...

qZero = [ ] , qOne = [ 120, 122] , qTwo = [221]

Back into the ArrayList and done..

Nerd_I_Bea at 2007-7-16 13:39:27 > top of Java-index,Java Essentials,Java Programming...
# 2

Modified a bit... but I am still having major issues Type Casting... any suggestions?

import java.util.*;

import java.io.*;

public class RadixSort

{

public static void main(String[] args) throws IOException

{

ArrayList<Integer> aList = new ArrayList<Integer>();

File file = new File("in.dat");

PrintWriter outFile = new PrintWriter(new File("out.dat"));

Scanner fileIn = new Scanner(file);

while (fileIn.hasNextInt())

{

int a = fileIn.nextInt();

aList.add(a);

}

System.out.println(aList);

System.out.println(rOrder(aList, 3));

}

public static ArrayList rOrder(ArrayList<Integer> aList, int n)

{

ArrayList<Integer> arr = new ArrayList<Integer>();

Queue<String> qZero = new LinkedList<String>();

Queue<String> qOne = new LinkedList<String>();

Queue<String> qTwo = new LinkedList<String>();

Queue<String> qThree = new LinkedList<String>();

Queue<String> qFour = new LinkedList<String>();

Queue<String> qFive = new LinkedList<String>();

Queue<String> qSix = new LinkedList<String>();

Queue<String> qSeven = new LinkedList<String>();

Queue<String> qEight = new LinkedList<String>();

Queue<String> qNine = new LinkedList<String>();

String data = "";

int x = 0;

while (!aList.isEmpty() && n > -1)

{

data = (String) aList.remove(n);

if (data.charAt(n) == '0')

qZero.offer(data);

else if (data.charAt(n) == '1')

qOne.offer(data);

else if (data.charAt(n) == '2')

qTwo.offer(data);

else if (data.charAt(n) == '3')

qThree.offer(data);

else if (data.charAt(n) == '4')

qFour.offer(data);

else if (data.charAt(n) == '5')

qFive.offer(data);

else if (data.charAt(n) == '6')

qSix.offer(data);

else if (data.charAt(n) == '7')

qSeven.offer(data);

else if (data.charAt(n) == '8')

qEight.offer(data);

else

qNine.offer(data);

data = "";

}

if (n < 0)

return arr;

else

{

while(!qZero.isEmpty())

arr.add((int)qZero.poll());

while(!qOne.isEmpty())

arr.add((int)qOne.poll());

while(!qTwo.isEmpty())

arr.add((int)qTwo.poll());

while(!qThree.isEmpty())

arr.add((int)qThree.poll());

while(!qFour.isEmpty())

arr.add((int)qFour.poll());

while(!qFive.isEmpty())

arr.add((int)qFive.poll());

while(!qSix.isEmpty())

arr.add((int)qSix.poll());

while(!qSeven.isEmpty())

arr.add((int)qSeven.poll());

while(!qEight.isEmpty())

arr.add((int)qEight.poll());

while(!qNine.isEmpty())

arr.add((int)qNine.poll());

rOrder(arr, n-1);

}

}

}

Nerd_I_Bea at 2007-7-16 13:39:27 > top of Java-index,Java Essentials,Java Programming...
# 3

Modified again... and I am fairly sure this version would work if I could figure out the problem with my type casting.

That's the only errors I am getting right now. It's saying that they are inconvertible types... is there something I am messing up with the type casting?

Thanks...

import java.util.*;

import java.io.*;

public class RadixSort

{

public static void main(String[] args) throws IOException

{

ArrayList<Integer> aList = new ArrayList<Integer>();

File file = new File("in.dat");

PrintWriter outFile = new PrintWriter(new File("out.dat"));

Scanner fileIn = new Scanner(file);

while (fileIn.hasNextInt())

{

int a = fileIn.nextInt();

aList.add(a);

}

System.out.println(aList);

System.out.println(rOrder(aList, 2));

}

public static ArrayList rOrder(ArrayList<Integer> aList, int n)

{

Queue<String> qZero = new LinkedList<String>();

Queue<String> qOne = new LinkedList<String>();

Queue<String> qTwo = new LinkedList<String>();

Queue<String> qThree = new LinkedList<String>();

Queue<String> qFour = new LinkedList<String>();

Queue<String> qFive = new LinkedList<String>();

Queue<String> qSix = new LinkedList<String>();

Queue<String> qSeven = new LinkedList<String>();

Queue<String> qEight = new LinkedList<String>();

Queue<String> qNine = new LinkedList<String>();

String data = "";

int y = 0;

int x = 0;

while (!aList.isEmpty() && n > -1)

{

y = aList.remove(n);

data = (String) y;

if (data.charAt(n) == '0')

qZero.offer(data);

else if (data.charAt(n) == '1')

qOne.offer(data);

else if (data.charAt(n) == '2')

qTwo.offer(data);

else if (data.charAt(n) == '3')

qThree.offer(data);

else if (data.charAt(n) == '4')

qFour.offer(data);

else if (data.charAt(n) == '5')

qFive.offer(data);

else if (data.charAt(n) == '6')

qSix.offer(data);

else if (data.charAt(n) == '7')

qSeven.offer(data);

else if (data.charAt(n) == '8')

qEight.offer(data);

else

qNine.offer(data);

data = "";

}

if (n < 0)

return arr;

else

{

while(!qZero.isEmpty())

aList.add((int)qZero.poll());

while(!qOne.isEmpty())

aList.add((int)qOne.poll());

while(!qTwo.isEmpty())

aList.add((int)qTwo.poll());

while(!qThree.isEmpty())

aList.add((int)qThree.poll());

while(!qFour.isEmpty())

aList.add((int)qFour.poll());

while(!qFive.isEmpty())

aList.add((int)qFive.poll());

while(!qSix.isEmpty())

aList.add((int)qSix.poll());

while(!qSeven.isEmpty())

aList.add((int)qSeven.poll());

while(!qEight.isEmpty())

aList.add((int)qEight.poll());

while(!qNine.isEmpty())

aList.add((int)qNine.poll());

rOrder(aList, n-1);

}

}

}

Nerd_I_Bea at 2007-7-16 13:39:27 > top of Java-index,Java Essentials,Java Programming...