Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. /**
  5. * Created by Matthew Gilliard
  6. * Date: 10/07/11
  7. * Time: 18:22
  8. */
  9. public class SortedRespectDue {
  10.  
  11. public static void main(String... args) {
  12.  
  13. Random r = new Random();
  14.  
  15. int[] maxThresholds = {2, 3, 5, 7, 10, 15, 20, 25, 35, 50, 75, 150, 300, 600};
  16.  
  17. System.out.print("size");
  18. for (int k : maxThresholds) {
  19. System.out.print(", T=" + k);
  20. }
  21. System.out.println();
  22.  
  23. for (double d = 1; d < 10000000; d *= 1.1) {
  24.  
  25. int i = (int) d;
  26.  
  27. Long[] array = new Long[i];
  28.  
  29. for (int j = 0; j < i; j++) {
  30. array[j] = r.nextLong();
  31. }
  32.  
  33. System.out.print(i);
  34. for (int k : maxThresholds) {
  35.  
  36. Long[] sortMe = new Long[i];
  37. System.arraycopy(array, 0, sortMe, 0, i);
  38.  
  39. long start = System.currentTimeMillis();
  40. sort(sortMe, k);
  41. System.out.print(", " + (System.currentTimeMillis() - start));
  42. }
  43. System.out.println();
  44.  
  45. }
  46.  
  47. }
  48.  
  49. public static void sort(Object[] a, int threshold) {
  50. Object[] aux = (Object[]) a.clone();
  51. mergeSort(aux, a, 0, a.length, 0, threshold);
  52. }
  53.  
  54. private static void mergeSort(Object[] src,
  55. Object[] dest,
  56. int low,
  57. int high,
  58. int off,
  59. int threshold) {
  60. int length = high - low;
  61.  
  62. // Insertion sort on smallest arrays
  63. if (length < threshold) {
  64. for (int i = low; i < high; i++)
  65. for (int j = i; j > low &&
  66. ((Comparable) dest[j - 1]).compareTo(dest[j]) > 0; j--)
  67. swap(dest, j, j - 1);
  68. return;
  69. }
  70.  
  71. // Recursively sort halves of dest into src
  72. int destLow = low;
  73. int destHigh = high;
  74. low += off;
  75. high += off;
  76. int mid = (low + high) >>> 1;
  77. mergeSort(dest, src, low, mid, -off, threshold);
  78. mergeSort(dest, src, mid, high, -off, threshold);
  79.  
  80. // If list is already sorted, just copy from src to dest. This is an
  81. // optimization that results in faster sorts for nearly ordered lists.
  82. if (((Comparable) src[mid - 1]).compareTo(src[mid]) <= 0) {
  83. System.arraycopy(src, low, dest, destLow, length);
  84. return;
  85. }
  86.  
  87. // Merge sorted halves (now in src) into dest
  88. for (int i = destLow, p = low, q = mid; i < destHigh; i++) {
  89. if (q >= high || p < mid && ((Comparable) src[p]).compareTo(src[q]) <= 0)
  90. dest[i] = src[p++];
  91. else
  92. dest[i] = src[q++];
  93. }
  94. }
  95.  
  96. /**
  97. * Swaps x[a] with x[b].
  98. */
  99. private static void swap(Object[] x, int a, int b) {
  100. Object t = x[a];
  101. x[a] = x[b];
  102. x[b] = t;
  103. }
  104.  
  105. }
Add Comment
Please, Sign In to add comment