Hi all -
I need to write a program that reads in an array of strings from a file and uses a radixSort method to sort and print the array based on what characters they are composed of. The implementation should be able to handle strings of characters. The characters are from the following character set {!, (, ), ., <, >, ?, A, B, …, Z, [, ], a, b, …, z, {, } }.
The first line in the file is how many differnet elements are there, and the rest of the file are the strings to be read in.
With my program I am able to read a file and print the array as it is standard, but I can't figure out how to use radix sorting with strings and queues. Right now I am getting an EmptyQueueException when I try to run..my code so far is thus:
package programFive;
import queuepackage.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class RadixSort {
public static void main(String[] args) throws FileNotFoundException{
File inputFile = new File("testinput.txt");
Scanner sc = new Scanner(inputFile);
int arraySize = sc.nextInt();
String[] unsortedArray = new String[arraySize];
int n = unsortedArray.length;
int d;
for(int i = 0; i < unsortedArray.length; i++){
String arrayInput = sc.next();
unsortedArray[i] = new String(arrayInput);
}
String temp = unsortedArray[0];
for(int j = 1; j < n; j++){
if(unsortedArray[j].compareTo(temp) > 0){
temp = unsortedArray[j];
}
}
d = temp.length();
printArray(unsortedArray);
radixSort(unsortedArray, n, d);
printArray(unsortedArray);
sc.close();
}
public static void printArray(String[] unsortedArray){
for(int i = 0; i < unsortedArray.length; i++){
if (i < unsortedArray.length - 1)
System.out.print(unsortedArray[i] + ",");
else
System.out.print(unsortedArray[i]);
}
}
public static void radixSort(String[] unsortedArray, int n, int d){
Queue[] queue = new Queue[n];
for(int k = 0; k < n; k++){
queue[k] = new Queue();
}
for(int i = d - 1; i >= 0; i--){
for(int j = 0; j < n; j++){
}
}
for(int index = 0; index < n; index++){
for(int i = d - 1; i < 0; i++){
char digit = (unsortedArray[index].charAt(i));
int dig = Integer.parseInt(String.valueOf(digit));
queue[dig].enqueue(unsortedArray[index]);
}
}
for(int p = 0; p < n; p++){
unsortedArray[p] = (queue[p].dequeue()).toString();
}
int item = 0;
for(int queueNum = 0; queueNum < n; queueNum++){
while(!unsortedArray[queueNum].isEmpty()){
}
}
}
}