Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. boolean compare(int x, int y){
  2. if(/* your crazy compare condition */)
  3. return true;
  4. else return false;
  5. }
  6.  
  7. procedure bubbleSort( A : list of sortable items )
  8. repeat
  9. swapped = false
  10. for i = 1 to length(A) - 1 inclusive do:
  11. /* if this pair is out of order */
  12. if compare(A[i],A[i-1]) then // Notcie the compare instead of A[i] > A[i-1]
  13. /* swap them and remember something changed */
  14. swap( A[i-1], A[i] )
  15. swapped = true
  16. end if
  17. end for
  18. until not swapped
  19. end procedure
  20.  
  21. import java.util.function.LongBinaryOperator;
  22.  
  23. public class ArraySort {
  24. public static void sort(long[] x, LongBinaryOperator op) {
  25. sort1(x, 0, x.length, op);
  26. }
  27.  
  28. private static void sort1(long x[], int off, int len, LongBinaryOperator op) {
  29. if (len < 7) {
  30. for (int i=off; i<len+off; i++)
  31. // Use custom comparator for insertion sort fallback
  32. for (int j=i; j>off && (op.applyAsLong(x[j-1], x[j]) > 0); j--)
  33. swap(x, j, j-1);
  34. return;
  35. }
  36.  
  37. int m = off + (len >> 1);
  38. if (len > 7) {
  39. int l = off;
  40. int n = off + len - 1;
  41. if (len > 40) {
  42. int s = len/8;
  43. l = med3(x, l, l+s, l+2*s);
  44. m = med3(x, m-s, m, m+s);
  45. n = med3(x, n-2*s, n-s, n);
  46. }
  47. m = med3(x, l, m, n);
  48. }
  49. long v = x[m];
  50.  
  51. int a = off, b = a, c = off + len - 1, d = c;
  52. while(true) {
  53. // Use custom comparator for checking elements
  54. while (b <= c && (op.applyAsLong(x[b], v) <= 0)) {
  55. if (x[b] == v)
  56. swap(x, a++, b);
  57. b++;
  58. }
  59. // Use custom comparator for checking elements
  60. while (c >= b && (op.applyAsLong(x[c], v) >= 0)) {
  61. if (x[c] == v)
  62. swap(x, c, d--);
  63. c--;
  64. }
  65. if (b > c)
  66. break;
  67. swap(x, b++, c--);
  68. }
  69.  
  70. int s, n = off + len;
  71. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  72. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  73.  
  74. if ((s = b-a) > 1)
  75. sort1(x, off, s, op);
  76. if ((s = d-c) > 1)
  77. sort1(x, n-s, s, op);
  78. }
  79.  
  80. private static void swap(long x[], int a, int b) {
  81. long t = x[a];
  82. x[a] = x[b];
  83. x[b] = t;
  84. }
  85.  
  86. private static void vecswap(long x[], int a, int b, int n) {
  87. for (int i=0; i<n; i++, a++, b++)
  88. swap(x, a, b);
  89. }
  90.  
  91. private static int med3(long x[], int a, int b, int c) {
  92. return (x[a] < x[b] ?
  93. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  94. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  95. }
  96. }
  97.  
  98. import java.util.Arrays;
  99.  
  100. public class Main {
  101. public static void main(String[] args) {
  102. long x[] = {5, 5, 7, 1, 2, 5, 8, 9, 23, 5, 32, 45, 76};
  103. ArraySort.sort(x, (a, b) -> b - a); // sort descending
  104. System.out.println(Arrays.toString(x));
  105. }
  106. }
  107.  
  108. [76, 45, 32, 23, 9, 8, 7, 5, 5, 5, 5, 2, 1]
  109.  
  110. /**
  111. * Sorts the specified sub-array of integers into ascending order.
  112. */
  113. private static void sort1(int x[], int off, int len) {
  114. // Insertion sort on smallest arrays
  115. if (len < 7) {
  116. for (int i=off; i<len+off; i++)
  117. for (int j=i; j>off && x[j-1]>x[j]; j--)
  118. swap(x, j, j-1);
  119. return;
  120. }
  121.  
  122. // Choose a partition element, v
  123. int m = off + (len >> 1); // Small arrays, middle element
  124. if (len > 7) {
  125. int l = off;
  126. int n = off + len - 1;
  127. if (len > 40) { // Big arrays, pseudomedian of 9
  128. int s = len/8;
  129. l = med3(x, l, l+s, l+2*s);
  130. m = med3(x, m-s, m, m+s);
  131. n = med3(x, n-2*s, n-s, n);
  132. }
  133. m = med3(x, l, m, n); // Mid-size, med of 3
  134. }
  135. int v = x[m];
  136.  
  137. // Establish Invariant: v* (<v)* (>v)* v*
  138. int a = off, b = a, c = off + len - 1, d = c;
  139. while(true) {
  140. while (b <= c && x[b] <= v) {
  141. if (x[b] == v)
  142. swap(x, a++, b);
  143. b++;
  144. }
  145. while (c >= b && x[c] >= v) {
  146. if (x[c] == v)
  147. swap(x, c, d--);
  148. c--;
  149. }
  150. if (b > c)
  151. break;
  152. swap(x, b++, c--);
  153. }
  154.  
  155. // Swap partition elements back to middle
  156. int s, n = off + len;
  157. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  158. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  159.  
  160. // Recursively sort non-partition-elements
  161. if ((s = b-a) > 1)
  162. sort1(x, off, s);
  163. if ((s = d-c) > 1)
  164. sort1(x, n-s, s);
  165. }
  166.  
  167. /**
  168. * Swaps x[a] with x[b].
  169. */
  170. private static void swap(int x[], int a, int b) {
  171. int t = x[a];
  172. x[a] = x[b];
  173. x[b] = t;
  174. }
  175.  
  176. /**
  177. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  178. */
  179. private static void vecswap(int x[], int a, int b, int n) {
  180. for (int i=0; i<n; i++, a++, b++)
  181. swap(x, a, b);
  182. }
  183.  
  184. /**
  185. * Returns the index of the median of the three indexed integers.
  186. */
  187. private static int med3(int x[], int a, int b, int c) {
  188. return (x[a] < x[b] ?
  189. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  190. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  191. }
  192.  
  193. java.util.Arrays.sort(int[])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement