Advertisement
UniQuet0p1

Untitled

Oct 4th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 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. /** maximal array length */
  14. static final int MAX_SIZE = 512000;
  15.  
  16. /** number of competition rounds */
  17. static final int NUMBER_OF_ROUNDS = 4;
  18.  
  19. /**
  20. * Main method.
  21. *
  22. * @param args
  23. * command line parameters
  24. */
  25. public static void main(String[] args) {
  26. final double[] origArray = new double[MAX_SIZE];
  27. Random generator = new Random();
  28. for (int i = 0; i < MAX_SIZE; i++) {
  29. origArray[i] = generator.nextDouble()*1000.;
  30. }
  31. int rightLimit = MAX_SIZE / (int) Math.pow(2., NUMBER_OF_ROUNDS);
  32.  
  33. // Start a competition
  34. for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
  35. double[] acopy;
  36. long stime, ftime, diff;
  37. rightLimit = 2 * rightLimit;
  38. System.out.println();
  39. System.out.println("Length: " + rightLimit);
  40.  
  41. acopy = Arrays.copyOf(origArray, rightLimit);
  42. stime = System.nanoTime();
  43. insertionSort(acopy);
  44. ftime = System.nanoTime();
  45. diff = ftime - stime;
  46. System.out.printf("%34s%11d%n", "Insertion sort: time (ms): ", diff / 1000000);
  47. checkOrder(acopy);
  48.  
  49. acopy = Arrays.copyOf(origArray, rightLimit);
  50. stime = System.nanoTime();
  51. binaryInsertionSort(acopy);
  52. ftime = System.nanoTime();
  53. diff = ftime - stime;
  54. System.out.printf("%34s%11d%n", "Binary insertion sort: time (ms): ", diff / 1000000);
  55. checkOrder(acopy);
  56.  
  57. acopy = Arrays.copyOf(origArray, rightLimit);
  58. stime = System.nanoTime();
  59. mergeSort(acopy, 0, acopy.length);
  60. ftime = System.nanoTime();
  61. diff = ftime - stime;
  62. System.out.printf("%34s%11d%n", "Merge sort: time (ms): ", diff / 1000000);
  63. checkOrder(acopy);
  64.  
  65. acopy = Arrays.copyOf(origArray, rightLimit);
  66. stime = System.nanoTime();
  67. quickSort(acopy, 0, acopy.length);
  68. ftime = System.nanoTime();
  69. diff = ftime - stime;
  70. System.out.printf("%34s%11d%n", "Quicksort: time (ms): ", diff / 1000000);
  71. checkOrder(acopy);
  72.  
  73. acopy = Arrays.copyOf(origArray, rightLimit);
  74. stime = System.nanoTime();
  75. Arrays.sort(acopy);
  76. ftime = System.nanoTime();
  77. diff = ftime - stime;
  78. System.out.printf("%34s%11d%n", "Java API Arrays.sort: time (ms): ", diff / 1000000);
  79. checkOrder(acopy);
  80. }
  81. }
  82.  
  83. /**
  84. * Insertion sort.
  85. *
  86. * @param a
  87. * array to be sorted
  88. */
  89. public static void insertionSort(double[] a) {
  90. if (a.length < 2)
  91. return;
  92. for (int i = 1; i < a.length; i++) {
  93. double b = a[i];
  94. int j;
  95. for (j = i - 1; j >= 0; j--) {
  96. if (a[j] <= b)
  97. break;
  98. a[j + 1] = a[j];
  99. }
  100. a[j + 1] = b;
  101. }
  102. }
  103.  
  104. /**
  105. * Binary insertion sort.
  106. *
  107. * @param a
  108. * array to be sorted
  109. */
  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.  
  136.  
  137. // public static int binarySearch(double[] arr, double x) {
  138. // int l = 0, r = arr.length - 1;
  139. // while (l <= r) {
  140. // int m = l + (r - l) / 2;
  141. // if (arr[m] == x)
  142. // return m;
  143. // if (arr[m] < x)
  144. // l = m + 1;
  145. // else
  146. // r = m - 1;
  147. // }
  148. // return -1;
  149. // }
  150.  
  151. /**
  152. * Merge sort.
  153. *
  154. * @param array
  155. * array to be sorted
  156. * @param left
  157. * begin of an interval (included)
  158. * @param right
  159. * end of an interval (excluded)
  160. */
  161. public static void mergeSort(double[] array, int left, int right) {
  162. if (array.length < 2)
  163. return;
  164. if ((right - left) < 2)
  165. return;
  166. int k = (left + right) / 2;
  167. mergeSort(array, left, k);
  168. mergeSort(array, k, right);
  169. merge(array, left, k, right);
  170. }
  171.  
  172. /**
  173. * Merge two intervals.
  174. *
  175. * @param array
  176. * original
  177. * @param left
  178. * start1
  179. * @param k
  180. * start2 = end1
  181. * @param right
  182. * end2
  183. */
  184. static public void merge(double[] array, int left, int k, int right) {
  185. if (array.length < 2 || (right - left) < 2 || k <= left || k >= right)
  186. return;
  187. double[] tmp = new double[right - left];
  188. int n1 = left;
  189. int n2 = k;
  190. int m = 0;
  191. while (true) {
  192. if ((n1 < k) && (n2 < right)) {
  193. if (array[n1] > array[n2]) {
  194. tmp[m++] = array[n2++];
  195. } else {
  196. tmp[m++] = array[n1++];
  197. }
  198. } else {
  199. if (n1 >= k) {
  200. for (int i = n2; i < right; i++) {
  201. tmp[m++] = array[i];
  202. }
  203. break;
  204. } else {
  205. for (int i = n1; i < k; i++) {
  206. tmp[m++] = array[i];
  207. }
  208. break;
  209. }
  210. }
  211. }
  212. System.arraycopy(tmp, 0, array, left, right - left);
  213. }
  214.  
  215. /**
  216. * Sort a part of the array using quicksort method.
  217. *
  218. * @param array
  219. * array to be changed
  220. * @param l
  221. * starting index (included)
  222. * @param r
  223. * ending index (excluded)
  224. */
  225. public static void quickSort(double[] array, int l, int r) {
  226. if (array == null || array.length < 1 || l < 0 || r <= l)
  227. throw new IllegalArgumentException("quickSort: wrong parameters");
  228. if ((r - l) < 2)
  229. return;
  230. int i = l;
  231. int j = r - 1;
  232. double x = array[(i + j) / 2];
  233. do {
  234. while (array[i] < x)
  235. i++;
  236. while (x < array[j])
  237. j--;
  238. if (i <= j) {
  239. double tmp = array[i];
  240. array[i] = array[j];
  241. array[j] = tmp;
  242. i++;
  243. j--;
  244. }
  245. } while (i < j);
  246. if (l < j)
  247. quickSort(array, l, j + 1); // recursion for left part
  248. if (i < r - 1)
  249. quickSort(array, i, r); // recursion for right part
  250. }
  251.  
  252. /**
  253. * Check whether an array is ordered.
  254. *
  255. * @param a
  256. * sorted (?) array
  257. * @throws IllegalArgumentException
  258. * if an array is not ordered
  259. */
  260. static void checkOrder(double[] a) {
  261. if (a.length < 2)
  262. return;
  263. for (int i = 0; i < a.length - 1; i++) {
  264. if (a[i] > a[i + 1])
  265. throw new IllegalArgumentException(
  266. "array not ordered: " + "a[" + i + "]=" + a[i] + " a[" + (i + 1) + "]=" + a[i + 1]);
  267. }
  268. }
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement