# Radix Sort Problem

Page 1 of 1

## 0 Replies - 616 Views - Last Post: 14 April 2009 - 08:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=99359&amp;s=8c84c803a3fae85af900ca7ae5d074b5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EvanISOT

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 14-April 09

# Radix Sort Problem

Posted 14 April 2009 - 08:00 PM

```public static void radixSort(int[] array, int digits)
{
//Buckets is our structure to sort the numbers by their respective digits...10	 queues for 10 possible
//digits; currentNumString holds a current number with padded zeroes so we can assure that each number is
//treated as though it had three digits, and currentDigitHolder holds the value of the current digit of interest
//so we can know what priority to put the current number in.
DecimalFormat df = new DecimalFormat("000");
ArrayList<Integer>[] buckets = new ArrayList[10];
for(int i = 0; i < 10; i++)
buckets[i] = new ArrayList(100);

String currentNumString;
int currentDigitHolder;
//Outer loop iterates through each digit in the number, starting at length - 1 down to position 0.
for(int i = digits - 1; i >= 0; i--)
{
//inner for loop cycles through the array from 0 to length - 1
for(int j = 0; j < array.length; j++)
{
currentNumString = df.format(array[j]);
currentDigitHolder = Character.getNumericValue(currentNumString.charAt(i));
buckets[currentDigitHolder].add(array[j]);
}
//Now numbers have been sorted into their buckets based on the current digit we're dealing with,
//so now we must recombine the buckets into the array from bucket 0 to bucket 9.
//Now dequeue the buckets back into the array.
int index = 0;
for(int k = 0; k < buckets.length; k++)
{
for(int j = 0; j < buckets[k].size(); j++)
{
array[index] = buckets[k].remove(j).intValue();
index++;
}
}
}

}
```

I had this radix sort working perfeclty, but my professor told me I had to change from PriorityQueues to an array of ArrayLists, and now it will not sort the array properly. I believe I'm doing everything correctly in principle, but it won't sort.

Would you point me in the right direction?

Thanks,
Evan

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}