Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RadixSortString {
- public static void main(String[] args) throws FileNotFoundException{
- File inputFile = new File("testinput.txt");
- Scanner sc = new Scanner(inputFile);
- int stringLength = sc.nextInt();
- String[] stringArray = new String[stringLength];
- int numberofWords = stringArray.length;
- int i = 0;
- while (sc.hasNextLine()) {
- String arrayInput = sc.next();
- stringArray[i] = arrayInput;
- i++;
- }
- printArray(stringArray);
- stringRadixSort(stringArray, numberofWords);
- System.out.println();
- printArray(stringArray);
- 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 stringRadixSort(String[] myArray, int stringLength){
- int queue = 255;
- Queue[] queueArray = new Queue[256];
- for (int startQueueIndex = 0; startQueueIndex < queueArray.length; startQueueIndex++){
- queueArray[startQueueIndex] = new Queue();
- }
- for (int i = 0; i < stringLength; i++){
- for (int myArrayIndex = 0; myArrayIndex < myArray.length; myArrayIndex++){
- char character = (myArray[myArrayIndex].charAt(i));
- System.out.println(character);
- int asciiValue = getAscii(character); //this method returns the ascii value for a character
- System.out.println(asciiValue);
- queueArray[asciiValue].enqueue(new String(myArray[myArrayIndex]));
- }
- int sortIndex = 0;
- System.out.println(myArray[sortIndex]);
- for (int j = 0; j <= queue; j++){
- while(!queueArray[j].isEmpty()){
- myArray[sortIndex] = ((String) queueArray[j].dequeue());
- System.out.println(myArray[sortIndex]);
- sortIndex++;
- }
- }
- }
- }
- public static int getAscii(char character){
- int value = character;
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement