Advertisement
UniQuet0p1

Untitled

Oct 4th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 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. public static void binaryInsertionSort(double[] a) {
  111. {
  112. for (int i=1; i<a.length; i++)
  113. {
  114. double temp = a[i]; //значение, которое будет вставлено в левую часть массива
  115. int j,left=0,right=i;
  116.  
  117. while (left<right) // поисковик
  118. {
  119. int middle = (left+right)/2;
  120. if (a[middle]<=temp)
  121. left=middle+1;
  122. else
  123. right = middle;
  124. }
  125.  
  126. j = right; // индекс вставки
  127.  
  128. if (j<i) // сдвигание вверх промежуточные элементы массива
  129. System.arraycopy(a, j, a, j + 1, i - j);
  130. a[j] = temp;
  131. }
  132. }
  133. }
  134.  
  135.  
  136. /**
  137. * Merge sort.
  138. *
  139. * @param array
  140. * array to be sorted
  141. * @param left
  142. * begin of an interval (included)
  143. * @param right
  144. * end of an interval (excluded)
  145. */
  146. public static void mergeSort(double[] array, int left, int right) {
  147. if (array.length < 2)
  148. return;
  149. if ((right - left) < 2)
  150. return;
  151. int k = (left + right) / 2;
  152. mergeSort(array, left, k);
  153. mergeSort(array, k, right);
  154. merge(array, left, k, right);
  155. }
  156.  
  157. /**
  158. * Merge two intervals.
  159. *
  160. * @param array
  161. * original
  162. * @param left
  163. * start1
  164. * @param k
  165. * start2 = end1
  166. * @param right
  167. * end2
  168. */
  169. static public void merge(double[] array, int left, int k, int right) {
  170. if (array.length < 2 || (right - left) < 2 || k <= left || k >= right)
  171. return;
  172. double[] tmp = new double[right - left];
  173. int n1 = left;
  174. int n2 = k;
  175. int m = 0;
  176. while (true) {
  177. if ((n1 < k) && (n2 < right)) {
  178. if (array[n1] > array[n2]) {
  179. tmp[m++] = array[n2++];
  180. } else {
  181. tmp[m++] = array[n1++];
  182. }
  183. } else {
  184. if (n1 >= k) {
  185. for (int i = n2; i < right; i++) {
  186. tmp[m++] = array[i];
  187. }
  188. break;
  189. } else {
  190. for (int i = n1; i < k; i++) {
  191. tmp[m++] = array[i];
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. System.arraycopy(tmp, 0, array, left, right - left);
  198. }
  199.  
  200. /**
  201. * Sort a part of the array using quicksort method.
  202. *
  203. * @param array
  204. * array to be changed
  205. * @param l
  206. * starting index (included)
  207. * @param r
  208. * ending index (excluded)
  209. */
  210. public static void quickSort(double[] array, int l, int r) {
  211. if (array == null || array.length < 1 || l < 0 || r <= l)
  212. throw new IllegalArgumentException("quickSort: wrong parameters");
  213. if ((r - l) < 2)
  214. return;
  215. int i = l;
  216. int j = r - 1;
  217. double x = array[(i + j) / 2];
  218. do {
  219. while (array[i] < x)
  220. i++;
  221. while (x < array[j])
  222. j--;
  223. if (i <= j) {
  224. double tmp = array[i];
  225. array[i] = array[j];
  226. array[j] = tmp;
  227. i++;
  228. j--;
  229. }
  230. } while (i < j);
  231. if (l < j)
  232. quickSort(array, l, j + 1); // recursion for left part
  233. if (i < r - 1)
  234. quickSort(array, i, r); // recursion for right part
  235. }
  236.  
  237. /**
  238. * Check whether an array is ordered.
  239. *
  240. * @param a
  241. * sorted (?) array
  242. * @throws IllegalArgumentException
  243. * if an array is not ordered
  244. */
  245. static void checkOrder(double[] a) {
  246. if (a.length < 2)
  247. return;
  248. for (int i = 0; i < a.length - 1; i++) {
  249. if (a[i] > a[i + 1])
  250. throw new IllegalArgumentException(
  251. "array not ordered: " + "a[" + i + "]=" + a[i] + " a[" + (i + 1) + "]=" + a[i + 1]);
  252. }
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement