skilletwaffles

lab 25.1

Feb 13th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. import chn.util.*;
  2. import apcslib.Format;
  3. import java.util.*;
  4.  
  5. /**
  6. * Description of the Class
  7. *
  8. * @author David Cortes
  9. * @created Now, 2/2015
  10. */
  11. class Sorts
  12. {
  13. /**
  14. * Description of the Method
  15. *
  16. * @param list Description of Parameter
  17. */
  18. public static void selectionSort(int[] list)
  19. {
  20. int min, temp;
  21.  
  22. for (int outer = 0; outer < list.length - 1; outer++)
  23. {
  24. min = outer;
  25. for (int inner = outer + 1; inner < list.length; inner++)
  26. {
  27. if (list[inner] < list[min])
  28. {
  29. min = inner;
  30. }
  31. }
  32. //swap(list[outer], list[flag]);
  33. temp = list[outer];
  34. list[outer] = list[min];
  35. list[min] = temp;
  36. }
  37. }
  38. }
  39.  
  40. /**
  41. * Description of the Class
  42. *
  43. * @author G. Peck (David Cortes)
  44. * @created July 18, 2002
  45. */
  46. public class MergeTemplate
  47. {
  48. public void merge (int[] a, int[] b, int[] c)
  49. {
  50. int aMark = 0;
  51. int bMark= 0;
  52. while(aMark < a.length || bMark <b.length)
  53. {
  54.  
  55. while(aMark == a.length-1 && bMark<b.length)
  56. {
  57. c[aMark + bMark] = b[bMark];
  58. bMark++;
  59. }
  60. while(bMark == b.length-1 && aMark<a.length)
  61. {
  62. c[aMark + bMark] = a[aMark];
  63. aMark++;
  64. }
  65.  
  66. if(a[aMark]< b[bMark])
  67. {
  68. c[aMark + bMark] =b[bMark];
  69. bMark++;
  70. }
  71. if(b[bMark]< a[aMark])
  72. {
  73. c[aMark + bMark] = a[aMark];
  74. aMark++;
  75. }
  76.  
  77. }
  78. }
  79.  
  80. /**
  81. * Initializes an returns temp with random integers in the range
  82. * 1..largestInt
  83. *
  84. * @return an array of size specified by the user filled
  85. * with random numbers
  86. */
  87. public int[] fillArray()
  88. {
  89. ConsoleIO console = new ConsoleIO();
  90.  
  91. System.out.println();
  92. System.out.print("How many numbers do you wish to generate? ");
  93. int numInts = console.readInt();
  94.  
  95. int[] temp = new int[numInts];
  96.  
  97. System.out.print("Largest integer to generate? ");
  98. int largestInt = console.readInt();
  99.  
  100. Random randGen = new Random();
  101.  
  102. for (int loop = 0; loop < temp.length; loop++)
  103. {
  104. temp[loop] = randGen.nextInt(largestInt) + 1;
  105. }
  106.  
  107. return temp;
  108. }
  109.  
  110. /**
  111. * prints out the contents of the array in tabular form, 20 columns
  112. */
  113. private void screenOutput(int[] temp)
  114. {
  115. for (int loop = 0; loop < temp.length; loop++)
  116. {
  117. if (loop % 15 == 0)
  118. {
  119. System.out.println();
  120. }
  121. System.out.print(Format.right(temp[loop], 5));
  122. }
  123. System.out.println();
  124. }
  125.  
  126. /**
  127. * Sorting Template:
  128. * Provides a main method for access to the sorting menu
  129. *
  130. * @param args The command line arguments - not used
  131. */
  132. public static void main(String[] args)
  133. {
  134. MergeTemplate testMerge = new MergeTemplate();
  135.  
  136. System.out.println("Filling list a");
  137. int[] a = testMerge.fillArray();
  138. System.out.println();
  139. System.out.println("Filling list b");
  140. int[] b = testMerge.fillArray();
  141.  
  142. int[] c = new int[a.length + b.length];
  143.  
  144. Sorts.selectionSort(a);
  145. Sorts.selectionSort(b);
  146. testMerge.merge(a,b,c);
  147.  
  148. System.out.println();
  149. System.out.println("list a: ");;
  150. testMerge.screenOutput(a);
  151. System.out.println();
  152. System.out.println();
  153. System.out.println("list b: ");;
  154. testMerge.screenOutput(b);
  155. System.out.println();
  156. System.out.println();
  157. System.out.println("list c: ");;
  158. testMerge.screenOutput(c);
  159. System.out.println();
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment