views:

632

answers:

1

Welcome. I have a radix sorting method that uses an array to go through, but has to have another array (bin) that will store in an empty queue. I am confused as to how I would make a queue for the bins. I also have a findPlace method that finds the place of the each digit when called upon. So, here is what I got. Can someone help me find what I am missing? Thanks so much for your time.

public static void radix(int [] list){
       int [] bin = new int[10]; 
      ArrayQueue<Integer> part = new ArrayQueue<Integer>(); // EDIT What would I do with this queue?? 
      int num = 0; 
         for(int i=0;i<list.length;i++)
         {
            bin[i] = 0;  
          }

            for(int pass=0;pass<list.length;pass++)
           {
                for(int num=0;num<list.length;num++)
               {
                       int digit=findPlace(bin[pass], num);
                 }

                  bin[digit].add(list[num]); // add to the bin
            }
              // Put back into list
               for(int h=0; h<10; h++)
               {
                    while(!bin[h].isEmpty())
                    {
                       list[num] = bin[queueNum].remove();
                     num++;
                    }
              }
        }


public static int getPlace (int x, int place)
    {return x/place % 10;}

I also made a method to find the bucket, So i just need to know how I would put it into an array, would I just do this? part.add(getPlace(x, place));?

+2  A: 

Your array, bin doesn't act like a queue just because you want it to :) Arrays don't have methods like add() and remove(). You have two choices on how to fix this:

  • Program in the proper handling for queues yourself: A queue consists of an array and two pointers into that array, traditionally called head and tail. You'd have to write your own methods to add and remove, which would work with the array and the pointers and take care of an empty queue or an overflowing one.

  • Use Java's built-in Queue class. It's documented in the library's Javadocs. Don't know if your assignment intends for you to build your own queue, though.

Update with another detail:

You asked how to extract a single digit from a number. It's easiest working from the least significant digits (LSD) as suggested in the Wikipedia article. To do that:

  • To extract the last digit, do digit = number % 10 (that's the modulo operation). You will get an integer between 0 and 9 inclusive.
  • To strip off the last digit, simply divide by 10. Then you can pull off another digit.
  • Since you'll need to be looking at the n-th last digit of a number several times, you would do well to put this functionality into a separate method.

You can use your 0 thru 9 to select the right queue to put your number on.

When you've finished queueing all your numbers into the 10 buckets, you need to copy them from there back into a single list. Then repeat as long as there are still digits in any number that you haven't processed.

Carl Smotricz
Yeah, you'd have to initialize the queue in your sorting method; it's probably a good idea to create the queue right there too. As for your algorithm, it looks wrong, but I'm not clever enough to say what it should look like.
Carl Smotricz
Well, I read the Wikipedia entry and found a section there on how to do radix sort using queues <http://en.wikipedia.org/wiki/Radix_sort#Iterative_version_using_queues> . According to that, you need not 1 but 10 queues. Have you read the description (or been given another description) and figured out what you need to do?
Carl Smotricz
Yes, but I don't want to do your homework for you. Whatever you're using for a queue, make an array of 10 of them. Also, see answer update, above.
Carl Smotricz
Where's this Array queue class you were talking about earlier? A decent queue implementation will have methods like add() and remove() you can use.
Carl Smotricz
I'm going to bed. Put up another question saying you need someone to talk you through implementing a radix sort for homework, with the extra challenge of very weak programming and problem solving skills on your side.
Carl Smotricz