Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class RadixSort {
  5.  
  6. private static ArrayList<LinkedList<String>> list = new ArrayList<LinkedList<String>>(27);
  7. private static ArrayList<String> unsorted = new ArrayList<String>();
  8. private static Scanner sc;
  9.  
  10. public static void sort(String filename) {
  11. init(filename);
  12.  
  13. // System.out.println(unsorted);
  14. for(int i=0; i<unsorted.size(); i++){
  15. System.out.println(unsorted.get(i));
  16. }
  17. //writeFile(filename);
  18. }
  19.  
  20.  
  21.  
  22. private static void writeFile(String str) {
  23.  
  24. //System.out.println(unsorted.size());
  25. //System.out.println(unsorted);
  26. try{
  27. File key = new File("answer.txt");
  28. PrintWriter sortWrite = new PrintWriter(key);
  29. sortWrite.println(str);
  30. sortWrite.close();
  31. System.out.println("Created File!");
  32. }catch(Exception e){
  33. System.out.println(e);
  34. System.out.println("Error 1");
  35. }
  36. }
  37.  
  38. private static void init(String filename) {
  39. try {
  40. File file = new File(filename);
  41. sc = new Scanner(file);
  42. } catch (Exception e) {
  43. // TODO Auto-generated catch block
  44. System.out.println(e);
  45. System.out.println("Error 2");
  46. }
  47. list.clear();
  48.  
  49. initList();
  50.  
  51. int maxLength = 0;
  52. while (sc.hasNext()) {
  53. String str = sc.next();
  54. if (str.length() > maxLength)
  55. maxLength = str.length();
  56. }
  57.  
  58. try {
  59. File file = new File(filename);
  60. sc = new Scanner(file);
  61. } catch (Exception e) {
  62. // TODO Auto-generated catch block
  63. System.out.println(e);
  64. System.out.println("Error 3");
  65. }
  66.  
  67. while (sc.hasNextLine()) {
  68. String str = sc.nextLine();
  69. unsorted.add(str);
  70. }
  71. // System.out.println(unsorted.size());
  72. // System.out.println(unsorted);
  73. sortHelper(maxLength);
  74. }
  75.  
  76. private static void sortHelper(int maxLength) {
  77. int index = maxLength - 1;
  78. while (index >= 0) {
  79. for (String input : unsorted) {
  80. String temp = formatString(input, maxLength);
  81. list.get(hashChar(temp.charAt(index))).add(input);
  82. }
  83. reAddToUnsorted();
  84. index--;
  85. }
  86. }
  87.  
  88. private static String formatString(String input, int maxLength) {
  89. String str = input;
  90. if (str.length() < maxLength) {
  91. for (int i = str.length(); i < maxLength; i++) {
  92. str = str + " ";
  93. }
  94. }
  95. return str.toLowerCase();
  96. }
  97.  
  98. private static void initList() {
  99. for (int i = 0; i < 27; i++) {
  100. list.add(new LinkedList<String>());
  101. }
  102. }
  103.  
  104. private static void reAddToUnsorted() {
  105. unsorted.clear();
  106. for (LinkedList<String> linked : list) {
  107. for (String str : linked) {
  108. unsorted.add(str);
  109. }
  110. linked.clear();
  111. }
  112. }
  113.  
  114. private static int hashChar(char c) {
  115. int ascii = (int) c;
  116.  
  117. if (c == ' ')
  118. return 0;
  119.  
  120. return ascii - 96;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement