skilletwaffles

lab 25.2

Feb 25th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.54 KB | None | 0 0
  1. import chn.util.*;
  2. import apcslib.Format;
  3. import java.util.*;
  4. /**
  5. * Description of the Class
  6. *
  7. * @David Cortes
  8. * @Right Now, 2015
  9. */
  10. class Sorts
  11. {
  12. private static long steps = 0;
  13. /**
  14. * Will sort a list of integers from least to greatest using
  15. * bubble sort method
  16. *
  17. * @param list list of random integers to be sorted
  18. */
  19. public static void bubbleSort(int[] list)
  20. {
  21. steps ++; //initialization of outer loop
  22. for (int outer = 0; outer < list.length - 1; outer++)
  23. {
  24. steps += 3; //boundary check, increment, initialization of inner loop
  25. for (int inner = 0; inner < list.length-outer-1; inner++)
  26. {
  27. steps += 3; //boundary check, increment, if comparison
  28. if (list[inner] > list[inner + 1])
  29. {
  30. //swap list[inner] & list[inner+1]
  31. int temp = list[inner];
  32. list[inner] = list[inner + 1];
  33. list[inner + 1] = temp;
  34. steps += 3; //swaping values
  35. }
  36. }
  37. }
  38. }
  39.  
  40. /**
  41. * Will sort a list of integers from least to greatest using
  42. * selection sort method
  43. *
  44. * @param list list of random integers to be sorted
  45. */
  46. public static void selectionSort(int[] list)
  47. {
  48. int min, temp;
  49. steps ++; //initialization of outer loop
  50. for (int outer = 0; outer < list.length - 1; outer++)
  51. {
  52. steps += 4; //boundary check, increment, intitialization of inner loop
  53. min = outer;
  54. for (int inner = outer + 1; inner < list.length; inner++)
  55. {
  56. steps +=3; //boundary check, increment, if comparison
  57. if (list[inner] < list[min])
  58. {
  59. min = inner; // a new smallest item is found
  60. steps ++;;
  61. }
  62. }
  63. //swap list[outer] & list[min]
  64. temp = list[outer];
  65. list[outer] = list[min];
  66. list[min] = temp;
  67. steps +=3;
  68. }
  69. }
  70.  
  71. /**
  72. * Will sort a list of integers from least to greaetest using
  73. * insertion sort method
  74. *
  75. * @param list list of random integers
  76. */
  77. public static void insertionSort(int[] list)
  78. {
  79. steps++; //initialization of outer loop
  80. for (int outer = 1; outer < list.length; outer++)
  81. {
  82. int position = outer;
  83. int key = list[position];
  84. // Shift larger values to the right
  85. steps +=5; //initialization of while loop
  86. while (position > 0 && list[position - 1] > key)
  87. {
  88. list[position] = list[position - 1];
  89. position--;
  90. steps++;
  91. }
  92. list[position] = key;
  93. steps++;
  94. }
  95. }
  96.  
  97. /**
  98. * Takes in entire vector, but will merge the following sections
  99. * together: Left sublist from a[first]..a[mid], right sublist from
  100. * a[mid+1]..a[last]. Precondition: each sublist is already in
  101. * ascending order
  102. *
  103. * @param a Description of Parameter
  104. * @param first Description of Parameter
  105. * @param mid Description of Parameter
  106. * @param last Description of Parameter
  107. */
  108. private static void merge(int[] a, int first, int mid, int last)
  109. {
  110. int[] temp = new int[a.length+1];
  111. int aMark = 0, bMark = mid +1;
  112. for(int i = 0; i<a.length; i++){
  113. if(aMark==mid && bMark < last) {
  114. temp[i] = a[bMark];
  115. bMark++;
  116. }
  117. else if(bMark == last && aMark < mid) {
  118. temp[i] = a[aMark];
  119. aMark++;
  120. }
  121. else if(aMark < mid && bMark<last) {
  122. if(a[aMark] < a[bMark]) {
  123. temp[i] = a[aMark];
  124. aMark++;
  125. }
  126. else {
  127. temp[i] = a[bMark];
  128. bMark++;
  129. }
  130. }
  131.  
  132. }
  133. a = temp;
  134. }
  135.  
  136. /**
  137. * Description of the Method
  138. *
  139. * @param a Description of Parameter
  140. * @param first Description of Parameter
  141. * @param last Description of Parameter
  142. */
  143. public static void mergeSort(int[] a, int first, int last)
  144. {
  145. int temp;
  146. int mid;
  147. if(last - first ==0){
  148. }
  149. else if(last - first == 1){
  150. if(a[first] >= a[last]){
  151. temp = a[first];
  152. a[first] = a[last];
  153. a[last] = temp;
  154. }
  155. }
  156.  
  157. else
  158. {
  159. mid = (first + last)/2;
  160. mergeSort(a, first, mid);
  161. mergeSort(a, mid+1, last);
  162. merge(a, first, mid+1, last);
  163.  
  164. }
  165. }
  166.  
  167. /**
  168. * Description of the Method
  169. *
  170. * @param list Description of Parameter
  171. * @param first Description of Parameter
  172. * @param last Description of Parameter
  173. */
  174. public static void quickSort(int[] list, int first, int last)
  175. {
  176. int g = first, h = last;
  177. int midIndex = (first + last) / 2;
  178. int dividingValue = list[midIndex];
  179. steps +=5;
  180. do
  181. {
  182. while (list[g] < dividingValue) g++;
  183. while (list[h] > dividingValue) h--;
  184. steps +=2;
  185. if (g <= h)
  186. {
  187. //swap(list[g], list[h]);
  188. int temp = list[g];
  189. list[g] = list[h];
  190. list[h] = temp;
  191. g++;
  192. h--;
  193. steps +=5;
  194. }
  195. }
  196. while (g < h);
  197. if (h > first){
  198. quickSort (list,first,h);
  199. steps++;
  200. }
  201. if (g < last){
  202. quickSort (list,g,last);
  203. steps++;
  204. }
  205. steps +=2;
  206. }
  207.  
  208. /**
  209. * Accessor method to return the current value of steps
  210. *
  211. */
  212. public static long getStepCount()
  213. {
  214. return steps;
  215. }
  216.  
  217. /**
  218. * Modifier method to set or reset the step count. Usually called
  219. * prior to invocation of a sort method.
  220. *
  221. * @param stepCount value assigned to steps
  222. */
  223. public static void setStepCount(int stepCount)
  224. {
  225. steps = stepCount;
  226. }
  227. }
  228. /**
  229. * Description of the Class
  230. *
  231. * @author G. Peck
  232. * @created July 18, 2002
  233. */
  234. public class SortStep
  235. {
  236. private ConsoleIO console = new ConsoleIO ();
  237. private int[] myArray;
  238. /**
  239. * Initializes myArray with random integers in the range
  240. * 1..largestInt
  241. *
  242. * @param numInts number of integers to generate (size of myArray)
  243. * @param largestInt largest possible random integer to create
  244. */
  245. private void fillArray(int numInts, int largestInt)
  246. {
  247. myArray = new int[numInts];
  248. Random randGen = new Random();
  249. for (int loop = 0; loop < myArray.length; loop++)
  250. {
  251. myArray[loop] = randGen.nextInt(largestInt) + 1;
  252. }
  253. }
  254.  
  255. /**
  256. * prints out the contents of the array in tabular form, 12 columns
  257. */
  258. private void screenOutput()
  259. {
  260. for (int loop = 0; loop < myArray.length; loop++)
  261. {
  262. if (loop % 12 == 0)
  263. {
  264. System.out.println();
  265. }
  266. System.out.print(Format.right(myArray[loop], 6));
  267. }
  268. System.out.println();
  269. }
  270.  
  271. /**
  272. * The program asks the user to select a sorting algorithm,
  273. * fills the array with an amount of data chosen by the user,
  274. * calls the sorting algorithm, and gives an option of printing
  275. * out the data after it has been sorted.
  276. */
  277. public void sortMenu()
  278. {
  279. String choice;
  280. String print;
  281. do
  282. {
  283. System.out.println();
  284. System.out.println("Sorting algorithm menu");
  285. System.out.println();
  286. System.out.println("(1) Bubble sort");
  287. System.out.println("(2) Selection sort");
  288. System.out.println("(3) Insertion sort");
  289. System.out.println("(4) Recursive mergesort");
  290. System.out.println("(5) Quicksort");
  291. System.out.println("(Q) Quit");
  292. System.out.println();
  293. System.out.print("Choice ---> ");
  294. choice = console.readLine() + " ";
  295. if ('1' <= choice.charAt(0) && choice.charAt(0) <= '5')
  296. {
  297. System.out.println();
  298. System.out.print("How many numbers do you wish to generate? ");
  299. int numInts = console.readInt();
  300. System.out.print("Largest integer to generate? ");
  301. int largestInt = console.readInt();
  302. fillArray(numInts, largestInt);
  303. Sorts.setStepCount(0);
  304. switch (choice.charAt(0))
  305. {
  306. case '1':
  307. Sorts.bubbleSort(myArray);
  308. break;
  309. case '2':
  310. Sorts.selectionSort(myArray);
  311. break;
  312. case '3':
  313. Sorts.insertionSort(myArray);
  314. break;
  315. case '4':
  316. Sorts.mergeSort(myArray, 0, myArray.length - 1);
  317. break;
  318. case '5':
  319. Sorts.quickSort(myArray, 0, myArray.length - 1);
  320. break;
  321. }
  322. System.out.println();
  323. System.out.print("Print list to screen (y/n)? ");
  324. print = console.readLine();
  325. if (print.charAt(0) == 'y' || print.charAt(0) == 'Y')
  326. {
  327. screenOutput();
  328. }
  329. System.out.println();
  330. System.out.println("# steps = " + Sorts.getStepCount());
  331. System.out.println();
  332. System.out.print("Hit return to continue");
  333. console.readLine();
  334. }
  335. } while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
  336. }
  337.  
  338. /**
  339. * Sorting Step Count Template:
  340. * Provides a main method for access to the sorting menu
  341. *
  342. * @param args The command line arguments - not used
  343. */
  344. public static void main(String[] args)
  345. {
  346. SortStep doSorts = new SortStep();
  347. doSorts.sortMenu();
  348. }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment