Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Radix Sort

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Radix Sort

    Hi guys,

    I have just finished implementing my radix sort. Seems to be working fine within the first and second pass, but not in the third and fourth pass. Anyone could please find what i am doing wrong!
    Please help me cause i really want to make this Radix Sort work!! I think I have some bugs in my program but I cant find where!!

    import java.util.Scanner;
    public class RadixSort 
    {
     
    	    public static void main(String[] args)
    		{
    			Scanner userinput = new Scanner(System.in);
    			System.out.print("Please enter the size of the array. ");
    			//User is prompted to enter the size of the array.
    			int count = userinput.nextInt();
     
    			int[] numbers = new int[count];
    			QueueLinkedList[] queue = new QueueLinkedList[10];
     
    			for(int i=0; i<queue.length; i++)
    			{
              		queue[i] = new QueueLinkedList();
            	}
     
    			//Generates random integers.
    			for(int i=0; i<numbers.length; i++)
    			{
    				numbers[i] = (int)(Math.random()*1001);
    				System.out.print(numbers[i]+", ");
    			}
      			System.out.println();
     
    			for(int j=1; j<=4; j++)
    			{
    				for(int i=0; i<numbers.length; i++)
    				{
    					int  temp = 0;
    					queue[getRadix(numbers[i],j)].enqueue(numbers[i]);
    				}
     
    				for(int i=0; i<queue.length; i++)
    				{
    					int index=0;
    					while(!queue[i].isEmpty())
    						{					
    							numbers[index] = queue[i].dequeueInt();
    							System.out.print(numbers[index]+", ");
    							index++;
    						}
    				}System.out.println();
    			}
     
    		}
        	public static int getRadix(int number, int radix)
        	{
    			int result = (int)(number / Math.pow(10,radix-1)) % 10;
    			return result;
    		}
     
     
    }

    Output of the program.
    --------------------Configuration: <Default>--------------------
    Please enter the size of the array. 20
    580, 152, 221, 85, 564, 79, 526, 692, 688, 635, 409, 440, 582, 81, 874, 440, 975, 840, 919, 856,
    580, 440, 440, 840, 221, 81, 152, 692, 582, 564, 874, 85, 635, 975, 526, 856, 688, 79, 409, 919,
    409, 409, 919, 919, 526, 635, 840, 440, 440, 840, 856, 564, 79, 79, 874, 975, 688, 582, 81, 692,
    81, 79, 81, 409, 440, 440, 582, 564, 526, 582, 692, 692, 688, 635, 874, 840, 856, 975, 975, 919,
    975, 975, 919, 635, 564, 79, 526, 692, 688, 635, 409, 440, 582, 81, 874, 440, 975, 840, 919, 856,

    Process completed.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Radix Sort

    Where are the comments in the code saying how it is going to solve the problem? If the algorithm were documented in the program, we could compare the code against the comments to see if the code was following the algorithm.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Radix Sort

    Also at javaranch and java-forums.org

Similar Threads

  1. Radix Sort
    By Ronald Spina in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 14th, 2012, 04:31 AM
  2. heap sort vs merge sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 3rd, 2012, 04:37 AM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. Radix sort Issues??? Help
    By dadof3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 13th, 2011, 01:08 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM