Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. public class RadixSortString {
  2. public static void main(String[] args) throws FileNotFoundException{
  3. File inputFile = new File("testinput.txt");
  4. Scanner sc = new Scanner(inputFile);
  5. int stringLength = sc.nextInt();
  6. String[] stringArray = new String[stringLength];
  7. int numberofWords = stringArray.length;
  8. int i = 0;
  9. while (sc.hasNextLine()) {
  10. String arrayInput = sc.next();
  11. stringArray[i] = arrayInput;
  12. i++;
  13. }
  14. printArray(stringArray);
  15. stringRadixSort(stringArray, numberofWords);
  16. System.out.println();
  17. printArray(stringArray);
  18. sc.close();
  19. }
  20. public static void printArray(String[] unsortedArray){
  21. for(int i = 0; i < unsortedArray.length; i++){
  22. if (i < unsortedArray.length - 1)
  23. System.out.print(unsortedArray[i] + " , ");
  24. else
  25. System.out.print(unsortedArray[i]);
  26. }
  27. }
  28. public static void stringRadixSort(String[] myArray, int stringLength){
  29. int queue = 255;
  30. Queue[] queueArray = new Queue[256];
  31. for (int startQueueIndex = 0; startQueueIndex < queueArray.length; startQueueIndex++){
  32. queueArray[startQueueIndex] = new Queue();
  33. }
  34.  
  35. for (int i = 0; i < stringLength; i++){
  36.  
  37. for (int myArrayIndex = 0; myArrayIndex < myArray.length; myArrayIndex++){
  38. char character = (myArray[myArrayIndex].charAt(i));
  39. System.out.println(character);
  40. int asciiValue = getAscii(character); //this method returns the ascii value for a character
  41. System.out.println(asciiValue);
  42. queueArray[asciiValue].enqueue(new String(myArray[myArrayIndex]));
  43. }
  44. int sortIndex = 0;
  45. System.out.println(myArray[sortIndex]);
  46. for (int j = 0; j <= queue; j++){
  47. while(!queueArray[j].isEmpty()){
  48. myArray[sortIndex] = ((String) queueArray[j].dequeue());
  49. System.out.println(myArray[sortIndex]);
  50. sortIndex++;
  51. }
  52. }
  53. }
  54. }
  55. public static int getAscii(char character){
  56. int value = character;
  57. return value;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement