Advertisement
UniQuet0p1

Untitled

Oct 4th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 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. System.out.println(temp);
  132. }
  133. }
  134. }
  135.  
  136.  
  137. /**
  138. * Merge sort.
  139. *
  140. * @param array
  141. * array to be sorted
  142. * @param left
  143. * begin of an interval (included)
  144. * @param right
  145. * end of an interval (excluded)
  146. */
  147. public static void mergeSort(double[] array, int left, int right) {
  148. if (array.length < 2)
  149. return;
  150. if ((right - left) < 2)
  151. return;
  152. int k = (left + right) / 2;
  153. mergeSort(array, left, k);
  154. mergeSort(array, k, right);
  155. merge(array, left, k, right);
  156. }
  157.  
  158. /**
  159. * Merge two intervals.
  160. *
  161. * @param array
  162. * original
  163. * @param left
  164. * start1
  165. * @param k
  166. * start2 = end1
  167. * @param right
  168. * end2
  169. */
  170. static public void merge(double[] array, int left, int k, int right) {
  171. if (array.length < 2 || (right - left) < 2 || k <= left || k >= right)
  172. return;
  173. double[] tmp = new double[right - left];
  174. int n1 = left;
  175. int n2 = k;
  176. int m = 0;
  177. while (true) {
  178. if ((n1 < k) && (n2 < right)) {
  179. if (array[n1] > array[n2]) {
  180. tmp[m++] = array[n2++];
  181. } else {
  182. tmp[m++] = array[n1++];
  183. }
  184. } else {
  185. if (n1 >= k) {
  186. for (int i = n2; i < right; i++) {
  187. tmp[m++] = array[i];
  188. }
  189. break;
  190. } else {
  191. for (int i = n1; i < k; i++) {
  192. tmp[m++] = array[i];
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. System.arraycopy(tmp, 0, array, left, right - left);
  199. }
  200.  
  201. /**
  202. * Sort a part of the array using quicksort method.
  203. *
  204. * @param array
  205. * array to be changed
  206. * @param l
  207. * starting index (included)
  208. * @param r
  209. * ending index (excluded)
  210. */
  211. public static void quickSort(double[] array, int l, int r) {
  212. if (array == null || array.length < 1 || l < 0 || r <= l)
  213. throw new IllegalArgumentException("quickSort: wrong parameters");
  214. if ((r - l) < 2)
  215. return;
  216. int i = l;
  217. int j = r - 1;
  218. double x = array[(i + j) / 2];
  219. do {
  220. while (array[i] < x)
  221. i++;
  222. while (x < array[j])
  223. j--;
  224. if (i <= j) {
  225. double tmp = array[i];
  226. array[i] = array[j];
  227. array[j] = tmp;
  228. i++;
  229. j--;
  230. }
  231. } while (i < j);
  232. if (l < j)
  233. quickSort(array, l, j + 1); // recursion for left part
  234. if (i < r - 1)
  235. quickSort(array, i, r); // recursion for right part
  236. }
  237.  
  238. /**
  239. * Check whether an array is ordered.
  240. *
  241. * @param a
  242. * sorted (?) array
  243. * @throws IllegalArgumentException
  244. * 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