Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. import edu.princeton.cs.algs4.*;
  2. import java.util.*;
  3. import java.lang.*;
  4. /**
  5. *
  6. * @author Gustaf Bjering
  7. *
  8. * Following program tests and measures the execution times for sorting algorithms
  9. * insertionsort, mergesort and quicksort for different input sizes N and computes an
  10. * average for these. Arrays are generated by different seeds which is used for filling
  11. * the arrays to be sorted with random integers.
  12. */
  13. public class LEAssignment2Ver2 {
  14. static int N;
  15. static int K;
  16. static double[] execTimes = new double[3];
  17.  
  18.  
  19. public static void main(String[] args) {
  20.  
  21. System.out.println("Enter a the number of integers to generate for the array: \n");
  22. N = StdIn.readInt();
  23. int arg = Integer.parseInt(args[0]);
  24. K = arg;
  25.  
  26. System.out.println("Results for test with Insertion, Merge " +
  27. "and Quick sort with length " + N + ":");
  28.  
  29.  
  30. runAlgorithms();
  31.  
  32.  
  33. printResults();
  34.  
  35. }
  36.  
  37. // method takes an argument long as seed for Random function which generates different
  38. // values of ints and puts them to array and returns that array
  39. public static int[] generateArray(long seed) {
  40. int val;
  41. int[] arr = new int[N];
  42. for(int i = 0; i < N; i++) {
  43. Random generator = new Random(seed);
  44. val = Math.abs(generator.nextInt(K) + 1);
  45.  
  46. arr[i] = val;
  47. seed--;
  48. }
  49. return arr;
  50. }
  51.  
  52.  
  53.  
  54. public static void printResults() {
  55. System.out.println("Sorting " + N + " elements for mergeSort average time " + execTimes[0]);
  56. System.out.println("Sorting " + N + " elements for quickSort average time " + execTimes[1]);
  57. System.out.println("Sorting " + N + " elements for insertionSort average time " + execTimes[2] + "\n");
  58.  
  59. }
  60.  
  61.  
  62. // method that runs and measures execution times for sorting merge, quick and insertion sort
  63. // for 14 different sizes of arrays generated by different seeds
  64. public static void runAlgorithms() {
  65. int[] arr;
  66. // seed generated by function currentTimeMillis
  67. long seed = System.currentTimeMillis();
  68.  
  69. //test for mergesort
  70. arr = generateArray(seed);
  71. long start1 = System.nanoTime();
  72. mergeSort(arr);
  73. long end1 = System.nanoTime();
  74. double time1 = (((double) (end1 - start1)) * (Math.pow(10, -9)));
  75. execTimes[0] += time1;
  76.  
  77. //test for quicksort
  78. arr = generateArray(seed);
  79. long start2 = System.nanoTime();
  80. quickSort(arr, 0, arr.length - 1);
  81. long end2 = System.nanoTime();
  82. double time2 = (((double) (end2 - start2)) * (Math.pow(10, -9)));
  83. execTimes[1] += time2;
  84.  
  85.  
  86. //test for insertionsort
  87. arr = generateArray(seed);
  88. long start3 = System.nanoTime();
  89. insertionSort(arr);
  90. long end3 = System.nanoTime();
  91. double time3 = (((double) (end3 - start3)) * (Math.pow(10, -9)));
  92. execTimes[2] += time3;
  93.  
  94.  
  95. }
  96.  
  97.  
  98. public static void insertionSort(int[] arr) {
  99. int var;
  100. // iterates array through from second element of the array
  101. for (int i = 1; i < arr.length; i++) {
  102.  
  103. // swap adjacent elements at position j and j-1 until element at position
  104. // j is smaller than element at position j-1
  105. for (int j = i; j > 0 && (arr[j-1] > arr[j]); j--) {
  106. // swap adjacent elements
  107. var = arr[j-1];
  108. arr[j-1] = arr[j];
  109. arr[j] = var;
  110.  
  111. }
  112. }
  113. }
  114.  
  115.  
  116. private static int[] mergeSort(int[] arr) {
  117. // return if array only has one element
  118. if(arr.length <= 1) {
  119. return arr;
  120. }
  121.  
  122. int mid = arr.length / 2;
  123.  
  124.  
  125. int[] left = new int[mid];
  126. // if input array has un-even length assert the un-even part to second sub array
  127. int[] right = arr.length % 2 == 0 ? new int[mid] : new int[mid + 1];
  128.  
  129. // copy elements from input array to left and right sub array
  130. for(int i = 0; i < arr.length ; i++) {
  131. if(i <= mid-1) {
  132. left[i] = arr[i];
  133. } else {
  134. right[i - mid] = arr[i];
  135. }
  136. }
  137.  
  138. // continue to divide left and right sub array until all sub arrays has one element
  139. left = mergeSort(left);
  140. right = mergeSort(right);
  141.  
  142. // merge arrays
  143. return merge(left, right);
  144.  
  145. }
  146.  
  147. private static int[] merge(int[] left, int[] right) {
  148. int leftPoint, rightPoint, resPoint;
  149. leftPoint = rightPoint = resPoint = 0;
  150. int[] res = new int[left.length + right.length];
  151.  
  152. //if there is elements in either the left, or the right array
  153. while(leftPoint < left.length || rightPoint < right.length){
  154.  
  155. //if there exists elements in both arrays
  156. if(leftPoint < left.length && rightPoint < right.length) {
  157.  
  158.  
  159. //depending on which element that is greater, assert
  160. //the minor one to the resulting array and increment by 1
  161. if(left[leftPoint] < right[rightPoint]) {
  162. res[resPoint++] = left[leftPoint++];
  163. } else {
  164. res[resPoint++] = right[rightPoint++];
  165. }
  166. }
  167. else if(leftPoint < left.length) {
  168. res[resPoint++] = left[leftPoint++];
  169. }
  170. else if(rightPoint < right.length) {
  171. res[resPoint++] = right[rightPoint++];
  172. }
  173.  
  174. }
  175. return res;
  176. }
  177.  
  178.  
  179. private static void quickSort(int[] a, int lo, int hi) {
  180. if((a.length > 1) && (lo < hi)) {
  181.  
  182. int position = partition(a, lo, hi);
  183.  
  184. quickSort(a, lo, position - 1);
  185. quickSort(a, position + 1, hi);
  186. }
  187.  
  188. }
  189.  
  190. // partitions sub array left <----> right so that element the last element in
  191. // the array is at a position where elements right of it is greater and elements at left
  192. private static int partition(int[] a, int lo, int hi) {
  193.  
  194. int pivotVal = a[hi];
  195. int i = lo - 1;
  196.  
  197. for(int j = lo; j <= hi - 1; j++) {
  198. if(a[j] <= pivotVal) {
  199. i++;
  200.  
  201. int val = a[i];
  202. a[i] = a[j];
  203. a[j] = val;
  204. }
  205. }
  206.  
  207. int p = a[i + 1];
  208. a[i + 1] = a[hi];
  209. a[hi] = p;
  210.  
  211. return i + 1;
  212. }
  213.  
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement