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.