Thread: Radix Sort

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Radix Sort


    Hello guys, I am implementing a Radix Sort using an array of queues.
    So far I have created the array and print it and is working fine.
    I have also extracted the LSD from each integer.
    No my target is to store the integers it the queues according to the LSD. (This would be the first pass).
    This is supposed to be processed in the for looped between line 29 and 35.
    -- problem is that when I deque the integers are being displayed in the same way as at the beginning!
    I know my problem is in my enqueue but I don't know how to do it.
    Here is the code:-
    Code:
    import java.util.*;
    
    public class MainProgram
    {
    
        public static void main(String[] args)
        {
        	
    	Scanner userinput = new Scanner(System.in);
    	System.out.print("Please enter the size of the array. ");
    	int count = userinput.nextInt(); //user enters the size of the array.
    
    	int[] numbers = new int[count];  //array containing hte integers.
    	int r=0;
    	
       	QueueLinkedList[] queue = new QueueLinkedList[10]; //created an instance of the queue. 10 bins needed for the Radix Sort.
    	for(int i=0; i<queue.length; i++)
    		{
              	queue[i] = new QueueLinkedList();
            }
    
    	for(int index = 0; index < numbers.length; index++) //generating random integers.
    		{
    			numbers[index] = (int)(Math.random()*1001);
    			System.out.print(index+"    ");
    			System.out.println(numbers[index]);
    		}	
    	
    		for(int i=0; i<numbers.length; i++) //identifying the LSD and store the integers in queues accoring to the LSD.
    		{									//the mistake is in this loop!				
    			numbers[r] = (numbers[i] % 10)/1;
    		//	System.out.println(numbers[r]);
    		    queue[r].enqueue(numbers[i]);
    		    //queue[r].dequeue();
    		    
    		}
    		for(int i=0;i<queue.length;i++){  //deque and printing the integers. Here the integers should be sorted by the LSD. 1st PASS.
    			queue[r].dequeue();				//however output is noot good!
    		}
        }    
    }
    Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348

IMN logo majestic logo threadwatch logo seochat tools logo