Advertisement
UniQuet0p1

Untitled

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