0 Replies - 543 Views - Last Post: 14 April 2009 - 08:00 PM Rate Topic: -----

#1 EvanISOT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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