Advertisement
ShadowZek

Untitled

Mar 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import java.util.*;
  2. public class SortingBorts {
  3. public static final int MAX_ARRAY_SIZE = 1000;
  4. public static void main(String[] args)
  5. {
  6. //Taking input from user
  7. Scanner keyboard = new Scanner(System.in);
  8. //Print operator
  9. System.out.println("Enter any number of words, containing the word Bort."
  10. + "\nDoes not have to include Bort."
  11. + "\nI will sort these for you."
  12. + "\nEnter quit so I know you are done entering sentences.");
  13. //Initializing input array
  14. String[] sentences = new String[MAX_ARRAY_SIZE];
  15. //Variables for input
  16. boolean quit = false; int iterate = 0;
  17. while (!quit)
  18. {
  19. String input = keyboard.nextLine();
  20. if (input.equalsIgnoreCase("quit"))
  21. {
  22. quit = true;
  23. }
  24. else
  25. {
  26. sentences[iterate] = new String();
  27. sentences[iterate] = input;
  28. }
  29. iterate++;
  30. }
  31. /*
  32. * Initializing a seperate array to the valued size of the input
  33. * This is to eliminate the list of null elements from the final printing
  34. * New array is set to the input's iterator - 1, to accomodate for the quit it takes
  35. */
  36. String[] words = new String[iterate - 1];
  37. for (int i = 0; i < iterate - 1; i++)
  38. {
  39. words[i] = new String();
  40. words[i] = sentences[i];
  41. }
  42. //Making an integer array for number of borts in the String being read
  43. int[] borts = new int[words.length];
  44. //Reading amount of borts, plugging the count into bort array
  45. int bortCount = 0;
  46. for (int i = 0; i < words.length; i++)
  47. {
  48. for (int j = 0; j < words[i].length(); j++)
  49. {
  50. if (words[i].toLowerCase().charAt(j) == 't' &&
  51. words[i].toLowerCase().charAt(j - 1) == 'r' &&
  52. words[i].toLowerCase().charAt(j - 2) == 'o' &&
  53. words[i].toLowerCase().charAt(j - 3) == 'b')
  54. {
  55. bortCount++;
  56. }
  57. }
  58. borts[i] = bortCount;
  59. bortCount = 0;
  60. }
  61. //Sorting array
  62. quickSort(words, borts, 0, words.length - 1);
  63. //Printing out sorted array
  64. System.out.println("Here are the sorted strings:");
  65. for (int i = 0; i < words.length; i++)
  66. {
  67. System.out.println(words[i]);
  68. }
  69. keyboard.close();
  70. }
  71. //Quick sorting full array method
  72. public static void quickSort(String[] s, int[] a, int leftIndex, int rightIndex)
  73. {
  74. if (leftIndex < rightIndex)
  75. {
  76. int pIndex = partition(s, a, leftIndex, rightIndex);
  77. quickSort(s, a, leftIndex, pIndex - 1);
  78. quickSort(s, a, pIndex, rightIndex);
  79. }
  80. }
  81. //Method of sorting two words
  82. public static int partition(String[] s, int[] a, int leftIndex, int rightIndex)
  83. {
  84. int i = leftIndex; int j = rightIndex;
  85. int pivot = a[(leftIndex + rightIndex / 2)];
  86. while (i <= j)
  87. {
  88. while (a[i] < pivot)
  89. {
  90. i++;
  91. }
  92. while (a[j] > pivot)
  93. {
  94. j--;
  95. }
  96. if (i <= j)
  97. {
  98. String temp = s[i];
  99. int temp2 = a[i];
  100. s[i] = s[j];
  101. a[i] = a[j];
  102. s[j] = temp;
  103. a[j] = temp2;
  104. i++; j--;
  105. }
  106. }
  107. return i;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement