Reputation: 11
I don't completely understand radix sort so that is making this more difficult for me to write this program. I need to sort an array of strings that is read from a .txt file. I was able to read the file and enter the strings into an array. A string can contain letters or special characters. I need help with writing the code for sorting the array. It feels like I'm close to the having the correct code, but I'm stuck and don't know what else to do. It would be greatly appreciated if I could get help and point me in the right direction.
Each string has the same amount of characters. I am also using a stackPackage that contains a queue class. I need to modify the code the my professor gave me to sort strings.
Here is the code I started with:
import queuepackage.*;
public class Radix {
public static void main(String[] args) {
int[] array = {143,934,782,687,555,222,111,213,842,2000};
printArray(array);
radixSort(array, 1000);
printArray(array);
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ", ");
}
System.out.println();
}
public static void radixSort(int[] array, int maxPowerOf10) {
Queue[] queueArray = new Queue[10];
for (int queueNum = 0; queueNum < 10; queueNum++) {
queueArray[queueNum] = new Queue();
}
for (int powerOf10 = 1; powerOf10 <= maxPowerOf10; powerOf10 = powerOf10 * 10) {
for (int item = 0; item < array.length; item++) {
int digit = getDigit(array[item], powerOf10);
queueArray[digit].enqueue(new Integer(array[item]));
}
int item = 0;
for (int queueNum = 0; queueNum < 10; queueNum++) {
while (!queueArray[queueNum].isEmpty()) {
array[item] = ((Integer) queueArray[queueNum].dequeue()).intValue();
item++;
}
}
}
}
public static int getDigit(int number, int powerOf10) {
return (number/powerOf10)%10;
}
}
This is what I have.
length = length of the array
wordLen = length of a string in the array
Here is my current code of the radixSort method:
public static void radixSort(String[] array, int length, int wordLen) {
Queue[] queueArray = new Queue[256];
for (int queueNum = 0; queueNum < 256; queueNum++) {
queueArray[queueNum] = new Queue();
}
for (int len = 0; len < wordLen; len++) {
for (int item = 0; item < length; item++) {
int letter = array[item].charAt(len);
queueArray[letter].enqueue(new String(array[item]));
}
int item = 0;
for (int queueNum = 0; queueNum < 256; queueNum++) {
while (!queueArray[queueNum].isEmpty()) {
array[item] = ((String) queueArray[queueNum].dequeue()).toString();
item++;
}
}
}
}
Upvotes: 1
Views: 5508
Reputation: 11
Changing for (int len = 0; len < wordLen; len++)
into for (int len = wordLen - 1; len >= 0; len--)
worked and makes my program does what it is supposed to
Upvotes: 0
Reputation: 533
The only mistake in your code that I can see is that in RadixSort you wanna start with sorting by the least significant digit and move up to more significant digits as you go. In this case however you are starting at the left and going right which is the wrong way around for natural order String sorting.
This however can be easily fixed by changing for (int len = 0; len < wordLen; len++)
into for (int len = wordLen - 1; len >= 0; len--)
.
Upvotes: 0
Reputation: 66
It's almost working, you just need to iterate the word backwards, look at the second for loop
public static void radixSort(String[] array, int length, int wordLen) {
Queue[] queueArray = new Queue[256];
for (int queueNum = 0; queueNum < 256; queueNum++) {
queueArray[queueNum] = new Queue();
}
for (int len = wordLen-1; len >= 0; len--) {
for (int item = 0; item < length; item++) {
int letter = array[item].charAt(len);
queueArray[letter].enqueue(new String(array[item]));
}
int item = 0;
for (int queueNum = 0; queueNum < 256; queueNum++) {
while (!queueArray[queueNum].isEmpty()) {
array[item] = ((String) queueArray[queueNum].dequeue()).toString(); // Here you need to swap the
item++;
}
}
}
}
If you iterate it in the regular way you are losing the previous information, so the last characters are the most important
Good luck!
Upvotes: 1