Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class QuickM3 {
  5.  
  6. private static final int MEDIAN_OF_3_CUTOFF = 2;
  7.  
  8. private QuickM3() { }
  9.  
  10. /**
  11. * Rearranges the array in ascending order, using the natural order.
  12. * @param a the array to be sorted
  13. */
  14. public static void sort(Comparable[] a) {
  15. StdRandom.shuffle(a);
  16. sort(a, 0, a.length - 1);
  17. assert isSorted(a);
  18. }
  19.  
  20. // quicksort the subarray from a[lo] to a[hi]
  21. private static void sort(Comparable[] a, int lo, int hi) {
  22. if (hi <= lo) return;
  23. int j = partition(a, lo, hi);
  24. sort(a, lo, j-1);
  25. sort(a, j+1, hi);
  26. assert isSorted(a, lo, hi);
  27. }
  28.  
  29. // partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi]
  30. // and return the index j.
  31. private static int partition(Comparable[] a, int lo, int hi) {
  32. int n = hi - lo + 1;
  33. int i = lo;
  34. int j = hi + 1;
  35.  
  36. int m = median3(a, lo, lo + n/2, hi);
  37. exch(a, m, lo);
  38. Comparable v = a[lo];
  39.  
  40. while (true) {
  41.  
  42. // find item on lo to swap
  43. while (less(a[++i], v))
  44. if (i == hi) break;
  45.  
  46. // find item on hi to swap
  47. while (less(v, a[--j]))
  48. if (j == lo) break; // redundant since a[lo] acts as sentinel
  49.  
  50. // check if pointers cross
  51. if (i >= j) break;
  52.  
  53. exch(a, i, j);
  54. }
  55.  
  56. // put partitioning item v at a[j]
  57. exch(a, lo, j);
  58.  
  59. // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
  60. return j;
  61. }
  62.  
  63. private static int median3(Comparable[] a, int i, int j, int k) {
  64. return (less(a[i], a[j]) ?
  65. (less(a[j], a[k]) ? j : less(a[i], a[k]) ? k : i) :
  66. (less(a[k], a[j]) ? j : less(a[k], a[i]) ? k : i));
  67. }
  68.  
  69. /**
  70. * Rearranges the array so that {@code a[k]} contains the kth smallest key;
  71. * {@code a[0]} through {@code a[k-1]} are less than (or equal to) {@code a[k]}; and
  72. * {@code a[k+1]} through {@code a[n-1]} are greater than (or equal to) {@code a[k]}.
  73. *
  74. * @param a the array
  75. * @param k the rank of the key
  76. * @return the key of rank {@code k}
  77. */
  78. public static Comparable select(Comparable[] a, int k) {
  79. if (k < 0 || k >= a.length) {
  80. throw new IndexOutOfBoundsException("Selected element out of bounds");
  81. }
  82. StdRandom.shuffle(a);
  83. int lo = 0, hi = a.length - 1;
  84. while (hi > lo) {
  85. int i = partition(a, lo, hi);
  86. if (i > k) hi = i - 1;
  87. else if (i < k) lo = i + 1;
  88. else return a[i];
  89. }
  90. return a[lo];
  91. }
  92.  
  93.  
  94.  
  95. /***************************************************************************
  96. * Helper sorting functions.
  97. ***************************************************************************/
  98.  
  99. // is v < w ?
  100. private static boolean less(Comparable v, Comparable w) {
  101. return v.compareTo(w) < 0;
  102. }
  103.  
  104. // exchange a[i] and a[j]
  105. private static void exch(Object[] a, int i, int j) {
  106. Object swap = a[i];
  107. a[i] = a[j];
  108. a[j] = swap;
  109. }
  110.  
  111.  
  112. /***************************************************************************
  113. * Check if array is sorted - useful for debugging.
  114. ***************************************************************************/
  115. private static boolean isSorted(Comparable[] a) {
  116. return isSorted(a, 0, a.length - 1);
  117. }
  118.  
  119. private static boolean isSorted(Comparable[] a, int lo, int hi) {
  120. for (int i = lo + 1; i <= hi; i++)
  121. if (less(a[i], a[i-1])) return false;
  122. return true;
  123. }
  124.  
  125.  
  126. // print array to standard output
  127. private static void show(Comparable[] a) {
  128. for (int i = 0; i < a.length; i++) {
  129. StdOut.println(a[i]);
  130. }
  131. }
  132.  
  133. /**
  134. * Reads in a sequence of strings from standard input; quicksorts them;
  135. * and prints them to standard output in ascending order.
  136. * Shuffles the array and then prints the strings again to
  137. * standard output, but this time, using the select method.
  138. *
  139. * @param args the command-line arguments
  140. */
  141. public static void main(String[] args) {
  142. String[] a = StdIn.readAllStrings();
  143. QuickM3.sort(a);
  144. show(a);
  145. assert isSorted(a);
  146.  
  147. // shuffle
  148. StdRandom.shuffle(a);
  149.  
  150. // display results again using select
  151. StdOut.println();
  152. for (int i = 0; i < a.length; i++) {
  153. String ith = (String) QuickM3.select(a, i);
  154. StdOut.println(ith);
  155. }
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement