skilletwaffles

25.2 updated

Feb 26th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.89 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 aMark = first;
  111. int bMark = mid +1;
  112. int[] b = new int[a.length];
  113. for(int j = 0; j<a.length; j++)
  114. b[j] = a[j];
  115.  
  116. for(int i = first; i<last; i++)
  117. {
  118. if(aMark > mid)
  119. {
  120. b[i] = a[bMark];
  121. bMark++;
  122. }
  123. else
  124. if(bMark > last)
  125. {
  126. b[i] = a[aMark];
  127. aMark++;
  128. }
  129. else
  130. if(a[aMark] < a[bMark])
  131. {
  132. b[i] = a[aMark];
  133. aMark++;
  134. }
  135. else
  136. if(a[bMark] <= a[aMark])
  137. {
  138. b[i] = a[bMark];
  139. bMark++;
  140. }
  141.  
  142. }
  143.  
  144. for(int k = 0; k<b.length; k++)
  145. a[k] = b[k];
  146. }
  147.  
  148. /**
  149. * Description of the Method
  150. *
  151. * @param a Description of Parameter
  152. * @param first Description of Parameter
  153. * @param last Description of Parameter
  154. */
  155. public static void mergeSort(int[] a, int first, int last)
  156. {
  157. int temp;
  158. int mid;
  159. if(last - first ==0)
  160. {
  161. ;
  162. }
  163. else{
  164. if(last - first == 1)
  165. {
  166. if(a[first] >= a[last])
  167. {
  168. temp = a[first];
  169. a[first] = a[last];
  170. a[last] = temp;
  171. }
  172. }
  173. else
  174. {
  175. mid = (first + last)/2;
  176. mergeSort(a, first, mid);
  177. mergeSort(a, mid+1, last);
  178. merge(a, first, mid, last);
  179. }
  180. }
  181. }
  182.  
  183.  
  184. /**
  185. * Description of the Method
  186. *
  187. * @param list Description of Parameter
  188. * @param first Description of Parameter
  189. * @param last Description of Parameter
  190. */
  191. public static void quickSort(int[] list, int first, int last)
  192. {
  193. int g = first, h = last;
  194. int midIndex = (first + last) / 2;
  195. int dividingValue = list[midIndex];
  196. steps +=5;
  197. do
  198. {
  199. while (list[g] < dividingValue) g++;
  200. while (list[h] > dividingValue) h--;
  201. steps +=2;
  202. if (g <= h)
  203. {
  204. //swap(list[g], list[h]);
  205. int temp = list[g];
  206. list[g] = list[h];
  207. list[h] = temp;
  208. g++;
  209. h--;
  210. steps +=5;
  211. }
  212. }
  213. while (g < h);
  214. if (h > first){
  215. quickSort (list,first,h);
  216. steps++;
  217. }
  218. if (g < last){
  219. quickSort (list,g,last);
  220. steps++;
  221. }
  222. steps +=2;
  223. }
  224.  
  225. /**
  226. * Accessor method to return the current value of steps
  227. *
  228. */
  229. public static long getStepCount()
  230. {
  231. return steps;
  232. }
  233.  
  234. /**
  235. * Modifier method to set or reset the step count. Usually called
  236. * prior to invocation of a sort method.
  237. *
  238. * @param stepCount value assigned to steps
  239. */
  240. public static void setStepCount(int stepCount)
  241. {
  242. steps = stepCount;
  243. }
  244. }
  245. /**
  246. * Description of the Class
  247. *
  248. * @author G. Peck
  249. * @created July 18, 2002
  250. */
  251. public class SortStep
  252. {
  253. private ConsoleIO console = new ConsoleIO ();
  254. private int[] myArray;
  255. /**
  256. * Initializes myArray with random integers in the range
  257. * 1..largestInt
  258. *
  259. * @param numInts number of integers to generate (size of myArray)
  260. * @param largestInt largest possible random integer to create
  261. */
  262. private void fillArray(int numInts, int largestInt)
  263. {
  264. myArray = new int[numInts];
  265. Random randGen = new Random();
  266. for (int loop = 0; loop < myArray.length; loop++)
  267. {
  268. myArray[loop] = randGen.nextInt(largestInt) + 1;
  269. }
  270. }
  271.  
  272. /**
  273. * prints out the contents of the array in tabular form, 12 columns
  274. */
  275. private void screenOutput()
  276. {
  277. for (int loop = 0; loop < myArray.length; loop++)
  278. {
  279. if (loop % 12 == 0)
  280. {
  281. System.out.println();
  282. }
  283. System.out.print(Format.right(myArray[loop], 6));
  284. }
  285. System.out.println();
  286. }
  287.  
  288. /**
  289. * The program asks the user to select a sorting algorithm,
  290. * fills the array with an amount of data chosen by the user,
  291. * calls the sorting algorithm, and gives an option of printing
  292. * out the data after it has been sorted.
  293. */
  294. public void sortMenu()
  295. {
  296. String choice;
  297. String print;
  298. do
  299. {
  300. System.out.println();
  301. System.out.println("Sorting algorithm menu");
  302. System.out.println();
  303. System.out.println("(1) Bubble sort");
  304. System.out.println("(2) Selection sort");
  305. System.out.println("(3) Insertion sort");
  306. System.out.println("(4) Recursive mergesort");
  307. System.out.println("(5) Quicksort");
  308. System.out.println("(Q) Quit");
  309. System.out.println();
  310. System.out.print("Choice ---> ");
  311. choice = console.readLine() + " ";
  312. if ('1' <= choice.charAt(0) && choice.charAt(0) <= '5')
  313. {
  314. System.out.println();
  315. System.out.print("How many numbers do you wish to generate? ");
  316. int numInts = console.readInt();
  317. System.out.print("Largest integer to generate? ");
  318. int largestInt = console.readInt();
  319. fillArray(numInts, largestInt);
  320. Sorts.setStepCount(0);
  321. switch (choice.charAt(0))
  322. {
  323. case '1':
  324. Sorts.bubbleSort(myArray);
  325. break;
  326. case '2':
  327. Sorts.selectionSort(myArray);
  328. break;
  329. case '3':
  330. Sorts.insertionSort(myArray);
  331. break;
  332. case '4':
  333. Sorts.mergeSort(myArray, 0, myArray.length - 1);
  334. break;
  335. case '5':
  336. Sorts.quickSort(myArray, 0, myArray.length - 1);
  337. break;
  338. }
  339. System.out.println();
  340. System.out.print("Print list to screen (y/n)? ");
  341. print = console.readLine();
  342. if (print.charAt(0) == 'y' || print.charAt(0) == 'Y')
  343. {
  344. screenOutput();
  345. }
  346. System.out.println();
  347. System.out.println("# steps = " + Sorts.getStepCount());
  348. System.out.println();
  349. System.out.print("Hit return to continue");
  350. console.readLine();
  351. }
  352. } while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
  353. }
  354.  
  355. /**
  356. * Sorting Step Count Template:
  357. * Provides a main method for access to the sorting menu
  358. *
  359. * @param args The command line arguments - not used
  360. */
  361. public static void main(String[] args)
  362. {
  363. SortStep doSorts = new SortStep();
  364. doSorts.sortMenu();
  365. }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment