Advertisement
UniQuet0p1

Untitled

Oct 5th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4. * Comparison of sorting methods. The same array of double values is
  5. * used for all methods.
  6. *
  7. * @author Jaanus
  8. * @version 1.0
  9. * @since 1.6
  10. */
  11. public class DoubleSorting {
  12.  
  13. /**
  14. * maximal array length
  15. */
  16. static final int MAX_SIZE = 512000;
  17.  
  18. /**
  19. * number of competition rounds
  20. */
  21. static final int NUMBER_OF_ROUNDS = 4;
  22.  
  23. /**
  24. * Main method.
  25. *
  26. * @param args command line parameters
  27. */
  28. public static void main(String[] args) {
  29. final double[] origArray = new double[MAX_SIZE];
  30. Random generator = new Random();
  31. for (int i = 0; i < MAX_SIZE; i++) {
  32. origArray[i] = generator.nextDouble() * 1000.;
  33. }
  34. int rightLimit = MAX_SIZE / (int) Math.pow(2., NUMBER_OF_ROUNDS);
  35.  
  36. // Start a competition
  37. for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
  38. double[] acopy;
  39. long stime, ftime, diff;
  40. rightLimit = 2 * rightLimit;
  41. System.out.println();
  42. System.out.println("Length: " + rightLimit);
  43.  
  44. acopy = Arrays.copyOf(origArray, rightLimit);
  45. stime = System.nanoTime();
  46. insertionSort(acopy);
  47. ftime = System.nanoTime();
  48. diff = ftime - stime;
  49. System.out.printf("%34s%11d%n", "Insertion sort: time (ms): ", diff / 1000000);
  50. checkOrder(acopy);
  51.  
  52. acopy = Arrays.copyOf(origArray, rightLimit);
  53. stime = System.nanoTime();
  54. binaryInsertionSort(acopy);
  55. ftime = System.nanoTime();
  56. diff = ftime - stime;
  57. System.out.printf("%34s%11d%n", "Binary insertion sort: time (ms): ", diff / 1000000);
  58. checkOrder(acopy);
  59.  
  60. acopy = Arrays.copyOf(origArray, rightLimit);
  61. stime = System.nanoTime();
  62. mergeSort(acopy, 0, acopy.length);
  63. ftime = System.nanoTime();
  64. diff = ftime - stime;
  65. System.out.printf("%34s%11d%n", "Merge sort: time (ms): ", diff / 1000000);
  66. checkOrder(acopy);
  67.  
  68. acopy = Arrays.copyOf(origArray, rightLimit);
  69. stime = System.nanoTime();
  70. quickSort(acopy, 0, acopy.length);
  71. ftime = System.nanoTime();
  72. diff = ftime - stime;
  73. System.out.printf("%34s%11d%n", "Quicksort: time (ms): ", diff / 1000000);
  74. checkOrder(acopy);
  75.  
  76. acopy = Arrays.copyOf(origArray, rightLimit);
  77. stime = System.nanoTime();
  78. Arrays.sort(acopy);
  79. ftime = System.nanoTime();
  80. diff = ftime - stime;
  81. System.out.printf("%34s%11d%n", "Java API Arrays.sort: time (ms): ", diff / 1000000);
  82. checkOrder(acopy);
  83. }
  84. }
  85.  
  86. /**
  87. * Insertion sort.
  88. *
  89. * @param a array to be sorted
  90. */
  91. public static void insertionSort(double[] a) {
  92. if (a.length < 2)
  93. return;
  94. for (int i = 1; i < a.length; i++) {
  95. double b = a[i];
  96. int j;
  97. for (j = i - 1; j >= 0; j--) {
  98. if (a[j] <= b)
  99. break;
  100. a[j + 1] = a[j];
  101. }
  102. a[j + 1] = b;
  103. }
  104. }
  105.  
  106. /**
  107. * Binary insertion sort.
  108. *
  109. * @param a array to be sorted
  110. */
  111. // public static void binaryInsertionSort(double[] a) {
  112. // {
  113. // for (int i=1; i<a.length; i++)
  114. // {
  115. // double temp = a[i]; //значение, которое будет вставлено в левую часть массива
  116. // int j,left=0,right=i;
  117. //
  118. // while (left<right) // поисковик
  119. // {
  120. // int middle = (left+right)/2;
  121. // if (a[middle]<=temp)
  122. // left=middle+1;
  123. // else
  124. // right = middle;
  125. // }
  126. //
  127. // j = right; // индекс вставки
  128. //
  129. // if (j<i) // сдвигание вверх промежуточные элементы массива
  130. // System.arraycopy(a, j, a, j + 1, i - j);
  131. // a[j] = temp;
  132. // }
  133. // }
  134. // }
  135. public static void binaryInsertionSort(double[] a) { //рекурсия
  136. for (int i = 1; i < a.length; i++) {
  137. double x = a[i];
  138.  
  139. int j = Math.abs(
  140. Arrays.binarySearch(a, 0,
  141. i, x) + 1);
  142.  
  143. System.arraycopy(a, j,
  144. a, j + 1, i - j);
  145.  
  146. a[j] = x;
  147. }
  148. }
  149.  
  150. /**
  151. * Merge sort.
  152. *
  153. * @param array array to be sorted
  154. * @param left begin of an interval (included)
  155. * @param right end of an interval (excluded)
  156. */
  157. public static void mergeSort(double[] array, int left, int right) {
  158. if (array.length < 2)
  159. return;
  160. if ((right - left) < 2)
  161. return;
  162. int k = (left + right) / 2;
  163. mergeSort(array, left, k);
  164. mergeSort(array, k, right);
  165. merge(array, left, k, right);
  166. }
  167.  
  168. /**
  169. * Merge two intervals.
  170. *
  171. * @param array original
  172. * @param left start1
  173. * @param k start2 = end1
  174. * @param right end2
  175. */
  176. static public void merge(double[] array, int left, int k, int right) {
  177. if (array.length < 2 || (right - left) < 2 || k <= left || k >= right)
  178. return;
  179. double[] tmp = new double[right - left];
  180. int n1 = left;
  181. int n2 = k;
  182. int m = 0;
  183. while (true) {
  184. if ((n1 < k) && (n2 < right)) {
  185. if (array[n1] > array[n2]) {
  186. tmp[m++] = array[n2++];
  187. } else {
  188. tmp[m++] = array[n1++];
  189. }
  190. } else {
  191. if (n1 >= k) {
  192. for (int i = n2; i < right; i++) {
  193. tmp[m++] = array[i];
  194. }
  195. } else {
  196. for (int i = n1; i < k; i++) {
  197. tmp[m++] = array[i];
  198. }
  199. }
  200. break;
  201. }
  202. }
  203. System.arraycopy(tmp, 0, array, left, right - left);
  204. }
  205.  
  206. /**
  207. * Sort a part of the array using quicksort method.
  208. *
  209. * @param array array to be changed
  210. * @param l starting index (included)
  211. * @param r ending index (excluded)
  212. */
  213. public static void quickSort(double[] array, int l, int r) {
  214. if (array == null || array.length < 1 || l < 0 || r <= l)
  215. throw new IllegalArgumentException("quickSort: wrong parameters");
  216. if ((r - l) < 2)
  217. return;
  218. int i = l;
  219. int j = r - 1;
  220. double x = array[(i + j) / 2];
  221. do {
  222. while (array[i] < x)
  223. i++;
  224. while (x < array[j])
  225. j--;
  226. if (i <= j) {
  227. double tmp = array[i];
  228. array[i] = array[j];
  229. array[j] = tmp;
  230. i++;
  231. j--;
  232. }
  233. } while (i < j);
  234. if (l < j)
  235. quickSort(array, l, j + 1); // recursion for left part
  236. if (i < r - 1)
  237. quickSort(array, i, r); // recursion for right part
  238. }
  239.  
  240. /**
  241. * Check whether an array is ordered.
  242. *
  243. * @param a sorted (?) array
  244. * @throws IllegalArgumentException if an array is not ordered
  245. */
  246. static void checkOrder(double[] a) {
  247. if (a.length < 2)
  248. return;
  249. for (int i = 0; i < a.length - 1; i++) {
  250. if (a[i] > a[i + 1])
  251. throw new IllegalArgumentException(
  252. "array not ordered: " + "a[" + i + "]=" + a[i] + " a[" + (i + 1) + "]=" + a[i + 1]);
  253. }
  254. }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement