Advertisement
UniQuet0p1

Untitled

Oct 4th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 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 void binaryInsertionSort(double[] a) { //рекурсия
  138. // for (int i = 1; i < a.length; i++)
  139. // {
  140. // double x = a[i];
  141. //
  142. // int j = Math.abs(
  143. // Arrays.binarySearch(a, 0,
  144. // i, x) + 1);
  145. //
  146. // System.arraycopy(a, j,
  147. // a, j + 1, i - j);
  148. //
  149. // a[j] = x;
  150. // }
  151. // }
  152.  
  153. /**
  154. * Merge sort.
  155. *
  156. * @param array
  157. * array to be sorted
  158. * @param left
  159. * begin of an interval (included)
  160. * @param right
  161. * end of an interval (excluded)
  162. */
  163. public static void mergeSort(double[] array, int left, int right) {
  164. if (array.length < 2)
  165. return;
  166. if ((right - left) < 2)
  167. return;
  168. int k = (left + right) / 2;
  169. mergeSort(array, left, k);
  170. mergeSort(array, k, right);
  171. merge(array, left, k, right);
  172. }
  173.  
  174. /**
  175. * Merge two intervals.
  176. *
  177. * @param array
  178. * original
  179. * @param left
  180. * start1
  181. * @param k
  182. * start2 = end1
  183. * @param right
  184. * end2
  185. */
  186. static public void merge(double[] array, int left, int k, int right) {
  187. if (array.length < 2 || (right - left) < 2 || k <= left || k >= right)
  188. return;
  189. double[] tmp = new double[right - left];
  190. int n1 = left;
  191. int n2 = k;
  192. int m = 0;
  193. while (true) {
  194. if ((n1 < k) && (n2 < right)) {
  195. if (array[n1] > array[n2]) {
  196. tmp[m++] = array[n2++];
  197. } else {
  198. tmp[m++] = array[n1++];
  199. }
  200. } else {
  201. if (n1 >= k) {
  202. for (int i = n2; i < right; i++) {
  203. tmp[m++] = array[i];
  204. }
  205. break;
  206. } else {
  207. for (int i = n1; i < k; i++) {
  208. tmp[m++] = array[i];
  209. }
  210. break;
  211. }
  212. }
  213. }
  214. System.arraycopy(tmp, 0, array, left, right - left);
  215. }
  216.  
  217. /**
  218. * Sort a part of the array using quicksort method.
  219. *
  220. * @param array
  221. * array to be changed
  222. * @param l
  223. * starting index (included)
  224. * @param r
  225. * ending index (excluded)
  226. */
  227. public static void quickSort(double[] array, int l, int r) {
  228. if (array == null || array.length < 1 || l < 0 || r <= l)
  229. throw new IllegalArgumentException("quickSort: wrong parameters");
  230. if ((r - l) < 2)
  231. return;
  232. int i = l;
  233. int j = r - 1;
  234. double x = array[(i + j) / 2];
  235. do {
  236. while (array[i] < x)
  237. i++;
  238. while (x < array[j])
  239. j--;
  240. if (i <= j) {
  241. double tmp = array[i];
  242. array[i] = array[j];
  243. array[j] = tmp;
  244. i++;
  245. j--;
  246. }
  247. } while (i < j);
  248. if (l < j)
  249. quickSort(array, l, j + 1); // recursion for left part
  250. if (i < r - 1)
  251. quickSort(array, i, r); // recursion for right part
  252. }
  253.  
  254. /**
  255. * Check whether an array is ordered.
  256. *
  257. * @param a
  258. * sorted (?) array
  259. * @throws IllegalArgumentException
  260. * if an array is not ordered
  261. */
  262. static void checkOrder(double[] a) {
  263. if (a.length < 2)
  264. return;
  265. for (int i = 0; i < a.length - 1; i++) {
  266. if (a[i] > a[i + 1])
  267. throw new IllegalArgumentException(
  268. "array not ordered: " + "a[" + i + "]=" + a[i] + " a[" + (i + 1) + "]=" + a[i + 1]);
  269. }
  270. }
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement