This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Radix Sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Radix Sort " Watch "Radix Sort " New topic
Author

Radix Sort

Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
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!!


Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
Here is the output of the program.
Please enter the size of the array. 5
306, 392, 374, 493, 31,
31, 392, 493, 374, 306,
306, 31, 374, 392, 493,
31, 392, 374, 493, 493,
493, 493, 374, 493, 31,

Process completed.

I notice that numbers are also being duplicated in very pass!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

I'd suggest that at line 42 you also output the value of index for debugging purposes.
Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
I did and hmm there is something strange in the output!

145, 960, 490, 681, 257, 862, 377, 386, 943, 517,
0 960, 1 490, 0 681, 0 862, 0 943, 0 145, 0 386, 0 257, 1 377, 2 517,
0 517, 1 517, 0 943, 0 257, 1 257, 0 862, 0 377, 1 377, 0 681, 1 386,
0 257, 0 386, 1 377, 2 386, 0 517, 1 517, 0 681, 1 681, 0 862, 0 943,
0 943, 1 681, 2 386, 3 681, 4 257, 5 862, 6 377, 7 386, 8 943, 9 517,

Any suggestions of what happeing!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Ronald, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/66538-radix-sort.html

edit: http://www.javaprogrammingforums.com/whats-wrong-my-code/20587-radix-sort.html


luck, db
There are no new questions, but there may be new answers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Ronald Spina wrote:Any suggestions of what happeing!


Let's ask that differently: Why does index keep getting set back to zero?
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 576
    
    7

and as an extension of what Paul suggested.

you may want to add a print at say line 51 (print out the number, radix and result) just to verify everything is working as you expect.
Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
Ok sorry for posting the same treath to other forums.
I have added some print statements and added some comments.


I have noticed that the index in while loop (47-52) is resetting when passing from a queue to another.
This is the result of my program.
Bdw I made the outer loop to loop I time so I can tackle problem at a time.

Please enter the size of the array. 9
807, 463, 609, 979, 88, 684, 905, 475, 667, 9 ramdom integers stored in numbers. Fine!
807 7, 463 3, 609 9, 979 9, 88 8, 684 4, 905 5, 475 5, 667 7, Finding the LSD and storing them in the queues. Fine!
0 463, 0 684, 0 905, 1 475, 0 807, 1 667, 0 88, 0 609, 1 979, Dequeue the queues on after each other and store int back to numbers. Here is the problem.

Process completed.
Ronald Spina
Greenhorn

Joined: Jan 23, 2012
Posts: 26
Of course! int index=0; has to be outside the for loop!
 
 
subject: Radix Sort