Guest User

Untitled

a guest
Apr 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. final static int ARRAY_LENGTH = 1000;
  6. final static int RANDOM_RANGE = 1000;
  7. final static int OPT_CUTOFF = 10;
  8. final static int RUN_TIMES = 10;
  9.  
  10. public static void main(String[] args) {
  11. long startTime = 0, endTime = 0, totalTime = 0;
  12. Integer[] array = new Integer[ARRAY_LENGTH];
  13.  
  14. //efficiency run for QuickSort
  15. for (int i = 0; i < RUN_TIMES; i++) {
  16. //populate an array with 1000 random Integer
  17. array = createArray(ARRAY_LENGTH);
  18. startTime = System.nanoTime();
  19. //QuickSort
  20. //Note: I did not use Arrays.sort() for this function because it is
  21. //a "tuned" quicksort according to the Java api specifications.
  22. //I thought the optimization might skew the results of this project.
  23. quickSort(array, 0, array.length - 1);
  24. endTime = System.nanoTime();
  25. totalTime = totalTime + (endTime - startTime);
  26. }
  27. // for(int i = 1; i < array.length; i++)
  28. // System.out.println(array[i]);
  29. System.out.println("<<quickSort>>\nMean running time for "
  30. + RUN_TIMES + " runs: " + totalTime / RUN_TIMES);
  31.  
  32. totalTime = 0;
  33.  
  34. //efficiency run for OptQSort1
  35. for (int i = 0; i < RUN_TIMES; i++) {
  36. //populate an array with 1000 random Integer
  37. array = createArray(ARRAY_LENGTH);
  38. startTime = System.nanoTime();
  39. //optimized quickSort1
  40. OptQSort1(array, 0, array.length - 1);
  41. endTime = System.nanoTime();
  42. totalTime = totalTime + (endTime - startTime);
  43. }
  44. //for(int i = 1; i < array.length; i++)
  45. // System.out.println(array[i]);
  46. System.out.println("<<OptQSort1>>\nMean running time for "
  47. + RUN_TIMES + " runs: " + totalTime / RUN_TIMES);
  48.  
  49. totalTime = 0;
  50.  
  51. //efficiency run for OptQSort2
  52. for (int i = 0; i < RUN_TIMES; i++) {
  53. //populate an array with 1000 random Integer
  54. array = createArray(ARRAY_LENGTH);
  55. startTime = System.nanoTime();
  56. //optimized quickSort2
  57. OptQSort2(array, 0, array.length - 1);
  58. endTime = System.nanoTime();
  59. totalTime = totalTime + (endTime - startTime);
  60. }
  61. //for(int i = 1; i < array.length; i++)
  62. // System.out.println(array[i]);
  63. System.out.println("<<OptQSort2>>\nMean running time for "
  64. + RUN_TIMES + " runs: " + totalTime / RUN_TIMES);
  65. }
  66.  
  67. //this method creates an array of random integers of a specified size
  68. public static Integer[] createArray(int arraySize) {
  69. Integer[] array = new Integer[arraySize];
  70. Random rand = new Random();
  71.  
  72. //populate array with random integers.
  73. for (int i = 0; i < arraySize; i++) {
  74. array[i] = rand.nextInt(RANDOM_RANGE);
  75. //array[i] = i;
  76. }
  77.  
  78. return array;
  79. }
  80.  
  81. //this method uses a quick sort algorithm to sort an array.
  82. public static void quickSort(Integer[] array, int first, int last) {
  83. if (last > first) {
  84. //create partitions
  85. Integer pivotIndex = partition(array, first, last);
  86. quickSort(array, first, pivotIndex - 1);
  87. quickSort(array, pivotIndex + 1, last);
  88. }
  89. }
  90.  
  91. //This method finds the partitions for the quicksort methods.
  92. private static int partition(Integer[] array, int first, int last) {
  93.  
  94. Integer pivot = array[first];
  95. int low = first + 1;
  96. int high = last;
  97.  
  98. while (high > low) {
  99. while (low <= high && array[low] <= pivot) {
  100. low++;
  101. }
  102. while (low <= high && array[high] > pivot) {
  103. high--;
  104. }
  105.  
  106. if (high > low) {
  107. int temp = array[high];
  108. array[high] = array[low];
  109. array[low] = temp;
  110. }
  111. }
  112. while (high > first && array[high] >= pivot) {
  113. high--;
  114. }
  115.  
  116. if (pivot > array[high]) {
  117. array[first] = array[high];
  118. array[high] = pivot;
  119. return high;
  120. } else {
  121. return first;
  122. }
  123. }
  124.  
  125. //this method uses an optimized quick sort algorithm to sort an array.
  126. public static void OptQSort1(Integer[] array, int first, int last) {
  127. //create partitions
  128. Integer pivotIndex = partition(array, first, last);
  129. if (last - first > OPT_CUTOFF) {
  130. quickSort(array, first, pivotIndex - 1);
  131. quickSort(array, pivotIndex + 1, last);
  132. }
  133. insertionSort(array, first, pivotIndex - 1);
  134. insertionSort(array, pivotIndex + 1, last);
  135.  
  136. }
  137.  
  138. //this method uses an optimized quick sort algorithm to sort an array.
  139. public static void OptQSort2(Integer[] array, int first, int last) {
  140. if (last - first > OPT_CUTOFF) {
  141. //create partitions
  142. Integer pivotIndex = partition(array, first, last);
  143. quickSort(array, first, pivotIndex - 1);
  144. quickSort(array, pivotIndex + 1, last);
  145. }
  146. //insertion sort
  147. insertionSort(array, 0, array.length);
  148. }
  149.  
  150. //this method performs an insertion sort on the partially ordered arrays
  151. //from the quicksort methods.
  152. public static void insertionSort(Integer[] array, int first, int last) {
  153. for (int i = first + 1; i < last; i++) {
  154. Integer currentElement = array[i];
  155. int k;
  156. for (k = i; k > 0 && array[k - 1] > currentElement; k--) {
  157. array[k] = array[k - 1];
  158. }
  159. array[k] = currentElement;
  160. }
  161. }
  162. }
Add Comment
Please, Sign In to add comment